Premium lesson

Retrieving Records From Multiple Tables in Sequelize

Sequelize·3 min read·Jan 1, 2025

In Sequelize, the data of associated models can essentially be fetched in two ways, through eager loading and lazy loading.

Eager loading refers to querying all the data of a model and its associated model at once, which in SQL translates to a LEFT OUTER JOIN query.

Lazy loading, on the other hand, refers to querying the data of a model, and later on, querying the data of its associated model only when you really need it.

Eager loading

In Sequelize, eager loading is done by querying a model using query methods, such as findOne() and findAll(), and specifying the associated model through the include property of the configuration object:

await ModelA.findOne|findAll({  include: {    model: ModelB  }});

Where:

  • ModelA is a model instance associated with another model through the hasOne() or hasMany() method.

  • ModelB is a model instance associated with another model through the belongsTo() method.

icon light bulb key

Unlock the Build RESTful APIs in Node.js module

Learn how to build real database-backed and framework-powered RESTful APIs with MySQL, Sequelize, and Express.

You get immediate access to:

  • 37 focused lessons across MySQL, Sequelize, backend, and Express
  • 4 real-world projects with commented solutions
  • Ongoing updates to this bundle
  • Lifetime access to this bundle
Unlock this module