3.3.3: Advanced Sequelize Concepts

Learning Objectives

  1. Sequelize model aliases and custom foreign keys can make our app logic clearer, especially when our schemas involve models with multiple associations with any other model

  2. Sequelize eager loading can make our database querying more efficient by retrieving all data we need from multiple tables in a single query instead of multiple

  3. Sequelize validations and constraints can make our applications more robust by allowing us to prompt our users when input data is invalid, and preventing us from saving invalid data to our databases


The Sequelize concepts in this module will not be necessary for many applications, but they are commonly used in industry, especially for more complex apps with many features, more related data that is queried often, and greater requirements for data integrity.

These Sequelize concepts do not appear often in coding interviews, although related concepts have come up in interviews, such as how to perform joins in SQL queries and how to ensure data integrity in a database.

Association Aliases & Custom Foreign Keys

Sequelize allows us to define aliases for models in associations. This is especially helpful in situations where a model has multiple associations with another model, for example in our Carousell app where the Listing model has multiple associations with the User model, 1 for buyer and 1 for seller.

The following official Sequelize guide shares an example of a sports game model associated twice with a team model, once for home team and once for away team. Rocket implemented the listing, buyer and seller associations similarly in our Carousell reference solution.

  1. Notice the example specifies both aliases and custom foreign keys. In our experience, when using aliases it's better practice to specify a custom foreign key to have clearer control over Sequelize.

The following section in the guide shares all the different ways to define and uses Sequelize aliases and foreign keys.

  1. There is no good reason for now to specify a custom foreign key unless we are using Sequelize aliases

  2. Aliases are primarily useful for Sequelize relationship methods, because we will be able to use the relationship methods with alias names to query related data, which should clarify our app logic

Eager Loading

Eager loading is an optimisation and optional at Rocket Academy. We will always be able to retrieve the data we need with multiple, simpler queries. If you are keen to optimise the SQL data query times in your app, read on.

The following official Sequelize tutorial on Eager Loading demonstrates the syntax for loading multiple associated tables in a single query.

  1. Note the include syntax used to fetch associated model data with eager loading

  2. Note the eager-loaded associated model may be an object or array of objects depending on whether the association is singular or plural

  3. After "Fetching an Aliased association" section, the other sections in the "Basic example" are nice to know and not necessary for now

  4. Retrieving junction table attributes with eager loading for M-M relationships can be helpful when we have non-FK data in our junction tables, such as intensity in our person and personalities example from the Sequelize M-M Relationships submodule

  5. Including all nested associated models is discouraged for performance reasons, less we truly need all that data at once

  6. Everything from "including soft deleted records" to "Using findAndCountAll with includes" is optional, we will not need to read it in detail until we need it

Validations & Constraints

Sequelize validations and constraints make our applications more robust by preventing users from inputting invalid data, and preventing our application logic from saving invalid data to our database.

Sequelize validations and constraints are not necessary for our relatively simple applications at Rocket, but will be more important when we work at companies that require high levels of data integrity, especially those that work with financial or healthcare data.

Sequelize performs validations defined in models at the application level in JavaScript, and instructs SQL (Postgres in our case) to perform constraints defined in migrations at the database level. Validations typically check for requirements that can be verified without database queries, such as capitalisation, valid email, is numeric. Constraints typically check for requirements that require database queries, such as whether a value is unique in a given column.

When we use validations and constraints in our code, we can surround our Sequelize model methods with try-catch blocks to catch errors when validations or constraints fail. This allows us to gracefully let our users know what happened and how to fix it.

The following official Sequelize tutorial demonstrates the syntax of defining Sequelize validations and constraints.

  1. There are many validations we can apply in Sequelize

  2. We can define constraints in migrations using the same validations syntax

  3. Validations are preformed in the JavaScript environment while Constrains are considered at the SQL level.

  4. Apply validations and constraints to models as well as migration files for the best result.

New to Rocket Academy?

If you're not enrolled in Rocket's Bootcamp and visiting this page, check out our website to learn more about our Bootcamp course!

Last updated