Skip to content

Model Configuration

setPrimaryKey()

setPrimaryKey() — returns void

Available in: model Category: Miscellaneous Functions

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()

NameTypeRequiredDefaultDescription
propertystringyesProperty (or list of properties) to set as the primary key.
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");
    }
}