Model Configuration
setPrimaryKey()
Signature
Section titled “Signature”setPrimaryKey() — returns void
Available in: model
Category: Miscellaneous Functions
Description
Section titled “Description”The setPrimaryKey() function allows you to define which property (or properties) of a model represent the primary key in the database. This is crucial for Wheels to correctly handle CRUD operations, updates, and record lookups. For single-column primary keys, pass the property name as a string. For composite primary keys (multiple columns together form the key), pass a comma-separated list of property names. Alias: setPrimaryKeys()
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
property | string | yes | — | Property (or list of properties) to set as the primary key. |
Examples
Section titled “Examples”1. Single primary key
component extends="Model" {
function config() {
// The primary key for this table is `userID`
setPrimaryKey("userID");
}
}
2. Composite primary key
component extends="Model" {
function config() {
// The combination of `orderID` and `productID` uniquely identifies a record
setPrimaryKey("orderID,productID");
}
}
3. Using the alias setPrimaryKeys()
component extends="Model" {
function config() {
// Alias works the same as `setPrimaryKey()`
setPrimaryKeys("customerID");
}
}