Relational Databases and Sequelize Associations
Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication and more.
Types of Associations
Sequelize supports four types of associations that, when combined, create structural relationships:
- HasOne: One-to-One relationship where foreign key is defined on target.
- BelongsTo: One-to-One relationship where foreign key is defined on source.
- HasMany: One-to-Many relationship.
- BelongsToMany: Many-to-Many relationship using a junction table.
Example Code for Junction Table
Article.belongsToMany(Tag, { through: 'ArticleTags' });
Tag.belongsToMany(Article, { through: 'ArticleTags' });