Skip to content

monolayer-pg / schema / primaryKey

Function: primaryKey()

primaryKey<T, PK>(columns): PgPrimaryKey<T | PK, T | PK>

Defines a column or a group of columns, that can be used as a unique identifier for rows in the table.

Type Parameters

Type Parameter
T extends string
PK extends string

Parameters

ParameterType
columns(T | PK)[]

Returns

PgPrimaryKey<T | PK, T | PK>

Remarks

A primary key constraint is a special case of a unique contraint that also guarantees that all of the attributes within the primary key do not have null values.

A table can have at most one primary key.

Example

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

const dbSchema = schema({
  tables: {
    documents: table({
      columns: {
        id: integer().generatedAlwasyAsIdentity(),
      },
      constraints: {
        primaryKey: primaryKey(["id"]),
      },
    }),
  },
});

See

PostgreSQL docs: Primary Keys