Abstract
Optional
parent: Association<any, Model<any, any>, string, NormalizedAssociationOptions<string>>Abstract
accessorsAbstract
foreignReadonly
optionsReadonly
parentA reference to the association that created this one.
The identifier of the relation on the source model.
The type of the association. One of HasMany
, BelongsTo
, HasOne
, BelongsToMany
negate isMultiAssociation instead
Creating an associations can automatically create other associations. This returns the initial association that caused the creation of the descendant associations.
Static
isGenerated using TypeDoc
Creating associations in sequelize is done by calling one of the belongsTo / hasOne / hasMany / belongsToMany functions on a model (the source), and providing another model as the first argument to the function (the target).
Creating an association will add a foreign key constraint to the attributes. All associations use
CASCADE
on update andSET NULL
on delete, except for n:m, which also usesCASCADE
on delete.When creating associations, you can provide an alias, via the
as
option. This is useful if the same model is associated twice, or you want your association to be called something other than the name of the target model.As an example, consider the case where users have many pictures, one of which is their profile picture. All pictures have a
userId
, but in addition the user model also has aprofilePictureId
, to be able to easily load the user's profile picture.To get full control over the foreign key column added by sequelize, you can use the
foreignKey
option. It can either be a string, that specifies the name, or and object type definition, equivalent to those passed tosequelize.define
.The foreign key column in Picture will now be called
uid
instead of the defaultuserId
.This specifies that the
uid
column cannot be null. In most cases this will already be covered by the foreign key constraints, which sequelize creates automatically, but can be useful in case where the foreign keys are disabled, e.g. due to circular references (seeconstraints: false
below).When fetching associated models, you can limit your query to only load some models. These queries are written in the same way as queries to
find
/findAll
. To only get pictures in JPG, you can do:There are several ways to update and add new associations. Continuing with our example of users and pictures:
You don't have to pass in a complete object to the association functions, if your associated model has a single primary key:
In the example above we have specified that a user belongs to his profile picture. Conceptually, this might not make sense, but since we want to add the foreign key to the user model this is the way to do it.
Note how we also specified
foreignKeyConstraints: false
for profile picture. This is because we add a foreign key from user to picture (profilePictureId), and from picture to user (userId). If we were to add foreign keys to both, it would create a cyclic dependency, and sequelize would not know which table to create first, since user depends on picture, and picture depends on user. These kinds of problems are detected by sequelize before the models are synced to the database, and you will get an error along the lines ofError: Cyclic dependency found. 'users' is dependent of itself
. If you encounter this, you should either disable some constraints, or rethink your associations completely.