Skip to content

Check constraints

Check constraints are defined in the constraints object of table definition using the check function.

ts
import { integer, table, check } from "@monolayer/pg/schema";

export const books = table({
  columns: {
    id: integer(),
    price: integer(),
  },
  constraints: {
    check: check(sql`${sql.ref("price")} > 0`), 
  },
});

A check constraint can refer also to multiple columns:

ts
import { integer, table, check } from "@monolayer/pg/schema";

export const books = table({
  columns: {
    id: integer(),
    price: integer(),
    discount: integer(),
  },
  constraints: {
    check: check(  
      sql`${sql.ref("price")} > 0 AND ${sql.ref("discount")} >= 10`  
    ), 
  },
});