Associating Models

Other than using validations to ensure consistency within our database, we can also create associations between two tables to ensure symbiotic relationships are maintained and updated. Databases maintain these relationships by creating foreign key references that hold metadata as to which table and column the foreign key is associated with. This metadata is what maintains integrity for the database. If we were to update a foreign key’s value without a proper reference, we would have to perform a separate query to update all of the rows that contained a reference to the foreign key to its new value.

For instance, we have three tables: customers, products, and receipts. The receipts table would have two columns (in addition ...

Get Supercharging Node.js Applications with Sequelize now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.