Skip to content

Migrator

primaryKey()

primaryKey() — returns any

Available in: tabledefinition Category: Table Definition Functions

Used inside migration table definitions to define a primary key for the table. By default, it creates a single-column integer primary key, but you can customize the data type, size, precision, and whether it should auto-increment. If you need composite primary keys, you can call this method multiple times within the same table definition. Additionally, you can configure references to other tables, along with cascading behaviors for updates and deletes. Only available in the migrator CFC.

NameTypeRequiredDefaultDescription
namestringyes
typestringnointeger
autoIncrementbooleannofalse
limitnumericno
precisionnumericno
scalenumericno
referencesstringno
onUpdatestringno
onDeletestringno
1. Basic auto-incrementing integer primary key
t.primaryKey(name="id", autoIncrement=true);

2. Primary key with custom type
t.primaryKey(name="sku", type="string", limit=20);

3. Composite primary keys (order_id + product_id)
t.primaryKey(name="order_id", type="integer");
t.primaryKey(name="product_id", type="integer");

4. UUID primary key
t.primaryKey(name="session_id", type="uuid");

5. Primary key with foreign key reference
t.primaryKey(name="payment_id", autoIncrement=true);