Using advanced association options
Sequelize offers a variety of tricks to help communicate with your database’s relations properly. Some of these methods will help query associations in a more organized and ergonomic way. Other methods will offer us a way to compose the database’s schematics for more advanced relationship patterns. In this section, we will be going over examples of how to manage complex many-to-many relationships using the Super Many-to-Many pattern, define scoped associations, and query polymorphic associations.
Using scopes with associations
Scopes are a way to define a namespace with a default set of parameters, or parameters that override previously applied scopes to a query. Associations may have defined scopes to help with organization of the code base. A key difference between scopes for associations and models is that the association scope’s parameters are applicable for the WHERE
clause. Model scopes can define the WHERE
, LIMIT
...