hasMany
(1)belongsTo
or hasOne
(2)hasMany
relationshiphasMany
relationship.hasOne
relationshipbelongsTo
relationship is a hasOne
relationship. Taking the same example as before, the opposite of "an address belongsTo
a customer" is simply "a customerhasOne
address".belongsTo
relationshipbelongsTo
a customer.collectionName
, the foreign key should be collection_name_id
.
If this is not the case, check out the section below.belongsTo
relationship. Even though we recommend you modify your database structure to stay within foreign key conventions (pointing to an id), there is a way to specify how your tables are linked.fk_customername
of a table Address points to the field name
of a table Customer, add the following:belongsToMany
relationship (SQL only)belongsToMany
association is often used to set up a many-to-many relationship with another model. For this example, we will consider the models Projects
and Users
. A user can be part of many projects, and one project has many users. The junction table that will keep track of the associations will be called userProjects
, which will contain the foreign keys projectId and userId.belongsTo
is straight forward, we check if the referenced table of the foreign key is unique (unique constraint or primary key), then a belongsTo
association can be set between the two tables.hasMany
association.hasOne
association.belongsTo
relationship to the corresponding collection.hasMany
relationship to the corresponding collection.