Migrator
primaryKey()
Signature
Section titled “Signature”primaryKey() — returns any
Available in: tabledefinition
Category: Table Definition Functions
Description
Section titled “Description”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.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
name | string | yes | — | |
type | string | no | integer | |
autoIncrement | boolean | no | false | |
limit | numeric | no | — | |
precision | numeric | no | — | |
scale | numeric | no | — | |
references | string | no | — | |
onUpdate | string | no | — | |
onDelete | string | no | — |
Examples
Section titled “Examples”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);