Skip to content

Primary key

The primary key is defined in the constraints object of table definition using the primaryKey function.

Single column

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

export const users = table({
  name: "users",
  columns: {
    id: integer().generatedAlwaysAsIdentity(), 
  },
  constraints: {
    primaryKey: primaryKey(["id"]), 
  },
});

Multiple columns

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

export const books = table({
  columns: {
    id: integer().generatedAlwaysAsIdentity(), 
    locationId: integer(), 
  },
  constraints: {
    primaryKey: primaryKey(["id", "locationId"]), 
  },
});