Skip to content

Model Configuration

belongsTo()

belongsTo() — returns void

Available in: model Category: Association Functions

Sets up a belongsTo association between this model and another model. Use this when the current model contains a foreign key referencing another model. This establishes a one-to-many relationship from the perspective of the other model (i.e., this model “belongs to” a parent model).

NameTypeRequiredDefaultDescription
namestringyesGives the association a name that you refer to when working with the association (in the include argument to findAll, to name one example).
modelNamestringnoName of associated model (usually not needed if you follow Wheels conventions because the model name will be deduced from the name argument).
foreignKeystringnoForeign key property name (usually not needed if you follow Wheels conventions since the foreign key name will be deduced from the name argument).
joinKeystringnoColumn name to join to if not the primary key (usually not needed if you follow Wheels conventions since the join key will be the table’s primary key/keys).
joinTypestringnoinnerUse to set the join type when joining associated tables. Possible values are inner (for INNER JOIN) and outer (for LEFT OUTER JOIN).
1. Standard belongsTo association
// Specify that instances of this model belong to an author
belongsTo("author");

Wheels will automatically deduce the foreign key as authorId and the associated model as Author.

2. Custom foreign key and model name
// Foreign key does not follow convention
belongsTo(name = "bookWriter", modelName = "author", foreignKey = "authorId");

Useful when your database column names or model names deviate from Wheels conventions.

3. Specify LEFT OUTER JOIN
belongsTo(name = "publisher", joinType = "outer");