Skip to content

Migrator

primaryKey()

primaryKey() — returns any

Available in: tabledefinition Category: Table Definition Functions

Adds a primary key definition to the table. this method also allows for multiple primary keys. Accepts columnName / columnNames as aliases for name (per #2803) so the PK helper matches the argument-naming convention every other column helper in this file uses. The legacy name parameter keeps working — it is still what the body reads and what init() passes when adding the conventional id primary key.

NameTypeRequiredDefaultDescription
namestringno—Legacy parameter for the primary-key column name. New code should prefer columnName.
columnNamestringno—Modern singular alias for name (matches sibling column helpers).
columnNamesstringno—Modern plural alias for name. Accepted for muscle-memory parity with t.integer(columnNames=...) etc. NOTE: unlike sibling helpers, this does NOT accept a comma-separated list — primaryKey() always creates one PK column, so columnNames="a,b" produces a single column literally named a,b (not two PKs). For composite PKs call t.primaryKey() multiple times.
typestringnointeger
autoIncrementbooleannofalse
limitnumericno—
precisionnumericno—
scalenumericno—
referencesstringno—
referenceColumnstringnoid
onUpdatestringno—
onDeletestringno—