Model Class
primaryKey()
Signature
Section titled “Signature”primaryKey() — returns string
Available in: model
Category: Miscellaneous Functions
Description
Section titled “Description”Returns the name of the primary key for this model’s table.
This is determined through database introspection.
If composite primary keys have been used, they will both be returned in a list.
This function is also aliased as primaryKeys().
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
position | numeric | no | 0 | If you are accessing a composite primary key, pass the position of a single key to fetch. |
Examples
Section titled “Examples”// 1. Get the name of the primary key for the `employee` model (maps to the `employees` table by default)
keyName = model("employee").primaryKey();
// keyName -> "id"
// 2. Get all primary key column names for a model with a composite primary key
keys = model("orderItem").primaryKey();
// keys -> "orderId,productId"
// 3. Get only the first key of a composite primary key using the `position` argument
firstKey = model("orderItem").primaryKey(1);
// firstKey -> "orderId"
// 4. Use the `primaryKeys()` alias (preferred for readability with composite keys)
keys = model("orderItem").primaryKeys();
// keys -> "orderId,productId"