Skip to content

Model Configuration

setPrimaryKey()

setPrimaryKey() — returns void

Available in: model Category: Miscellaneous Functions

Allows you to pass in the name(s) of the property(s) that should be used as the primary key(s). Pass as a list if defining a composite primary key. This function is also aliased as setPrimaryKeys().

NameTypeRequiredDefaultDescription
propertystringyes—Property (or list of properties) to set as the primary key.
// 1. In `models/User.cfc`, define the primary key as a column called `userID`
//    instead of the Wheels default of `id`.
component extends="Model" {
	function config() {
		setPrimaryKey("userID");
	}
}

// 2. Define a composite primary key for a join model using two columns.
//    `setPrimaryKeys()` is an alias for `setPrimaryKey()` that reads more
//    naturally when multiple properties are involved.
component extends="Model" {
	function config() {
		table("users_roles");
		setPrimaryKeys("userID,roleID");
	}
}