Eager Loading

As briefly mentioned in the associations guide, eager Loading is the act of querying data of several models at once (one ‘main’ model and one or more associated models). At the SQL level, this is a query with one or more joins).

When this is done, the associated models will be added by Sequelize in appropriately named, automatically created field(s) in the returned objects.

In Sequelize, eager loading is mainly done by using the include option on a model finder query (such as findOne, findAll, etc).

Basic example

Let’s assume the following setup:

  1. const User = sequelize.define('user', { name: DataTypes.STRING }, { timestamps: false });
  2. const Task = sequelize.define('task', { name: DataTypes.STRING }, { timestamps: false });
  3. const Tool = sequelize.define('tool', {
  4. name: DataTypes.STRING,
  5. size: DataTypes.STRING
  6. }, { timestamps: false });
  7. User.hasMany(Task);
  8. Task.belongsTo(User);
  9. User.hasMany(Tool, { as: 'Instruments' });

Fetching a single associated element

OK. So, first of all, let’s load all tasks with their associated user:

  1. const tasks = await Task.findAll({ include: User });
  2. console.log(JSON.stringify(tasks, null, 2));

Output:

  1. [{
  2. "name": "A Task",
  3. "id": 1,
  4. "userId": 1,
  5. "user": {
  6. "name": "John Doe",
  7. "id": 1
  8. }
  9. }]

Here, tasks[0].user instanceof User is true. This shows that when Sequelize fetches associated models, they are added to the output object as model instances.

Above, the associated model was added to a new field called user in the fetched task. The name of this field was automatically chosen by Sequelize based on the name of the associated model, where its pluralized form is used when applicable (i.e., when the association is hasMany or belongsToMany). In other words, since Task.belongsTo(User), a task is associated to one user, therefore the logical choice is the singular form (which Sequelize follows automatically).

Fetching all associated elements

Now, instead of loading the user that is associated to a given task, we will do the opposite - we will find all tasks associated to a given user.

The method call is essentially the same. The only difference is that now the extra field created in the query result uses the pluralized form (tasks in this case), and its value is an array of task instances (instead of a single instance, as above).

  1. const users = await User.findAll({ include: Task });
  2. console.log(JSON.stringify(users, null, 2));

Output:

  1. [{
  2. "name": "John Doe",
  3. "id": 1,
  4. "tasks": [{
  5. "name": "A Task",
  6. "id": 1,
  7. "userId": 1
  8. }]
  9. }]

Notice that the accessor (the tasks property in the resulting instance) is pluralized since the association is one-to-many.

Fetching an Aliased association

If an association is aliased (using the as option), you must specify this alias when including the model. Instead of passing the model directly to the include option, you should instead provide an object with two options: model and as.

Notice how the user’s Tools are aliased as Instruments above. In order to get that right you have to specify the model you want to load, as well as the alias:

  1. const users = await User.findAll({
  2. include: { model: Tool, as: 'Instruments' }
  3. });
  4. console.log(JSON.stringify(users, null, 2));

Output:

  1. [{
  2. "name": "John Doe",
  3. "id": 1,
  4. "Instruments": [{
  5. "name": "Scissor",
  6. "id": 1,
  7. "userId": 1
  8. }]
  9. }]

You can also include by alias name by specifying a string that matches the association alias:

  1. User.findAll({ include: 'Instruments' }); // Also works
  2. User.findAll({ include: { association: 'Instruments' } }); // Also works

Required eager loading

When eager loading, we can force the query to return only records which have an associated model, effectively converting the query from the default OUTER JOIN to an INNER JOIN. This is done with the required: true option, as follows:

  1. User.findAll({
  2. include: {
  3. model: Task,
  4. required: true
  5. }
  6. });

This option also works on nested includes.

Eager loading filtered at the associated model level

When eager loading, we can also filter the associated model using the where option, as in the following example:

  1. User.findAll({
  2. include: {
  3. model: Tool,
  4. as: 'Instruments'
  5. where: {
  6. size: {
  7. [Op.ne]: 'small'
  8. }
  9. }
  10. }
  11. });

Generated SQL:

  1. SELECT
  2. `user`.`id`,
  3. `user`.`name`,
  4. `Instruments`.`id` AS `Instruments.id`,
  5. `Instruments`.`name` AS `Instruments.name`,
  6. `Instruments`.`size` AS `Instruments.size`,
  7. `Instruments`.`userId` AS `Instruments.userId`
  8. FROM `users` AS `user`
  9. INNER JOIN `tools` AS `Instruments` ON
  10. `user`.`id` = `Instruments`.`userId` AND
  11. `Instruments`.`size` != 'small';

Note that the SQL query generated above will only fetch users that have at least one tool that matches the condition (of not being small, in this case). This is the case because, when the where option is used inside an include, Sequelize automatically sets the required option to true. This means that, instead of an OUTER JOIN, an INNER JOIN is done, returning only the parent models with at least one matching children.

Note also that the where option used was converted into a condition for the ON clause of the INNER JOIN. In order to obtain a top-level WHERE clause, instead of an ON clause, something different must be done. This will be shown next.

Referring to other columns

If you want to apply a WHERE clause in an included model referring to a value from an associated model, you can simply use the Sequelize.col function, as show in the example below:

  1. // Find all projects with a least one task where task.state === project.state
  2. Project.findAll({
  3. include: {
  4. model: Task,
  5. where: {
  6. state: Sequelize.col('project.state')
  7. }
  8. }
  9. })

Complex where clauses at the top-level

To obtain top-level WHERE clauses that involve nested columns, Sequelize provides a way to reference nested columns: the '$nested.column$' syntax.

It can be used, for example, to move the where conditions from an included model from the ON condition to a top-level WHERE clause.

  1. User.findAll({
  2. where: {
  3. '$Instruments.size$': { [Op.ne]: 'small' }
  4. },
  5. include: [{
  6. model: Tool,
  7. as: 'Instruments'
  8. }]
  9. });

Generated SQL:

  1. SELECT
  2. `user`.`id`,
  3. `user`.`name`,
  4. `Instruments`.`id` AS `Instruments.id`,
  5. `Instruments`.`name` AS `Instruments.name`,
  6. `Instruments`.`size` AS `Instruments.size`,
  7. `Instruments`.`userId` AS `Instruments.userId`
  8. FROM `users` AS `user`
  9. LEFT OUTER JOIN `tools` AS `Instruments` ON
  10. `user`.`id` = `Instruments`.`userId`
  11. WHERE `Instruments`.`size` != 'small';

The $nested.column$ syntax also works for columns that are nested several levels deep, such as $some.super.deeply.nested.column$. Therefore, you can use this to make complex filters on deeply nested columns.

For a better understanding of all differences between the inner where option (used inside an include), with and without the required option, and a top-level where using the $nested.column$ syntax, below we have four examples for you:

  1. // Inner where, with default `required: true`
  2. await User.findAll({
  3. include: {
  4. model: Tool,
  5. as: 'Instruments',
  6. where: {
  7. size: { [Op.ne]: 'small' }
  8. }
  9. }
  10. });
  11. // Inner where, `required: false`
  12. await User.findAll({
  13. include: {
  14. model: Tool,
  15. as: 'Instruments',
  16. where: {
  17. size: { [Op.ne]: 'small' }
  18. },
  19. required: false
  20. }
  21. });
  22. // Top-level where, with default `required: false`
  23. await User.findAll({
  24. where: {
  25. '$Instruments.size$': { [Op.ne]: 'small' }
  26. },
  27. include: {
  28. model: Tool,
  29. as: 'Instruments'
  30. }
  31. });
  32. // Top-level where, `required: true`
  33. await User.findAll({
  34. where: {
  35. '$Instruments.size$': { [Op.ne]: 'small' }
  36. },
  37. include: {
  38. model: Tool,
  39. as: 'Instruments',
  40. required: true
  41. }
  42. });

Generated SQLs, in order:

  1. -- Inner where, with default `required: true`
  2. SELECT [...] FROM `users` AS `user`
  3. INNER JOIN `tools` AS `Instruments` ON
  4. `user`.`id` = `Instruments`.`userId`
  5. AND `Instruments`.`size` != 'small';
  6. -- Inner where, `required: false`
  7. SELECT [...] FROM `users` AS `user`
  8. LEFT OUTER JOIN `tools` AS `Instruments` ON
  9. `user`.`id` = `Instruments`.`userId`
  10. AND `Instruments`.`size` != 'small';
  11. -- Top-level where, with default `required: false`
  12. SELECT [...] FROM `users` AS `user`
  13. LEFT OUTER JOIN `tools` AS `Instruments` ON
  14. `user`.`id` = `Instruments`.`userId`
  15. WHERE `Instruments`.`size` != 'small';
  16. -- Top-level where, `required: true`
  17. SELECT [...] FROM `users` AS `user`
  18. INNER JOIN `tools` AS `Instruments` ON
  19. `user`.`id` = `Instruments`.`userId`
  20. WHERE `Instruments`.`size` != 'small';

Fetching with RIGHT OUTER JOIN (MySQL, MariaDB, PostgreSQL and MSSQL only)

By default, associations are loaded using a LEFT OUTER JOIN - that is to say it only includes records from the parent table. You can change this behavior to a RIGHT OUTER JOIN by passing the right option, if the dialect you are using supports it.

Currenly, SQLite does not support right joins.

Note: right is only respected if required is false.

  1. User.findAll({
  2. include: [{
  3. model: Task // will create a left join
  4. }]
  5. });
  6. User.findAll({
  7. include: [{
  8. model: Task,
  9. right: true // will create a right join
  10. }]
  11. });
  12. User.findAll({
  13. include: [{
  14. model: Task,
  15. required: true,
  16. right: true // has no effect, will create an inner join
  17. }]
  18. });
  19. User.findAll({
  20. include: [{
  21. model: Task,
  22. where: { name: { [Op.ne]: 'empty trash' } },
  23. right: true // has no effect, will create an inner join
  24. }]
  25. });
  26. User.findAll({
  27. include: [{
  28. model: Tool,
  29. where: { name: { [Op.ne]: 'empty trash' } },
  30. required: false // will create a left join
  31. }]
  32. });
  33. User.findAll({
  34. include: [{
  35. model: Tool,
  36. where: { name: { [Op.ne]: 'empty trash' } },
  37. required: false
  38. right: true // will create a right join
  39. }]
  40. });

Multiple eager loading

The include option can receive an array in order to fetch multiple associated models at once:

  1. Foo.findAll({
  2. include: [
  3. {
  4. model: Bar,
  5. required: true
  6. },
  7. {
  8. model: Baz,
  9. where: /* ... */
  10. },
  11. Qux // Shorthand syntax for { model: Qux } also works here
  12. ]
  13. })

Eager loading with Many-to-Many relationships

When you perform eager loading on a model with a Belongs-to-Many relationship, Sequelize will fetch the junction table data as well, by default. For example:

  1. const Foo = sequelize.define('Foo', { name: DataTypes.TEXT });
  2. const Bar = sequelize.define('Bar', { name: DataTypes.TEXT });
  3. Foo.belongsToMany(Bar, { through: 'Foo_Bar' });
  4. Bar.belongsToMany(Foo, { through: 'Foo_Bar' });
  5. await sequelize.sync();
  6. const foo = await Foo.create({ name: 'foo' });
  7. const bar = await Bar.create({ name: 'bar' });
  8. await foo.addBar(bar);
  9. const fetchedFoo = Foo.findOne({ include: Bar });
  10. console.log(JSON.stringify(fetchedFoo, null, 2));

Output:

  1. {
  2. "id": 1,
  3. "name": "foo",
  4. "Bars": [
  5. {
  6. "id": 1,
  7. "name": "bar",
  8. "Foo_Bar": {
  9. "FooId": 1,
  10. "BarId": 1
  11. }
  12. }
  13. ]
  14. }

Note that every bar instance eager loaded into the "Bars" property has an extra property called Foo_Bar which is the relevant Sequelize instance of the junction model. By default, Sequelize fetches all attributes from the junction table in order to build this extra property.

However, you can specify which attributes you want fetched. This is done with the attributes option applied inside the through option of the include. For example:

  1. Foo.findAll({
  2. include: [{
  3. model: Bar,
  4. through: {
  5. attributes: [/* list the wanted attributes here */]
  6. }
  7. }]
  8. });

If you don’t want anything from the junction table, you can explicitly provide an empty array to the attributes option, and in this case nothing will be fetched and the extra property will not even be created:

  1. Foo.findOne({
  2. include: {
  3. model: Bar,
  4. attributes: []
  5. }
  6. });

Output:

  1. {
  2. "id": 1,
  3. "name": "foo",
  4. "Bars": [
  5. {
  6. "id": 1,
  7. "name": "bar"
  8. }
  9. ]
  10. }

Whenever including a model from a Many-to-Many relationship, you can also apply a filter on the junction table. This is done with the where option applied inside the through option of the include. For example:

  1. User.findAll({
  2. include: [{
  3. model: Project,
  4. through: {
  5. where: {
  6. // Here, `completed` is a column present at the junction table
  7. completed: true
  8. }
  9. }
  10. }]
  11. });

Generated SQL (using SQLite):

  1. SELECT
  2. `User`.`id`,
  3. `User`.`name`,
  4. `Projects`.`id` AS `Projects.id`,
  5. `Projects`.`name` AS `Projects.name`,
  6. `Projects->User_Project`.`completed` AS `Projects.User_Project.completed`,
  7. `Projects->User_Project`.`UserId` AS `Projects.User_Project.UserId`,
  8. `Projects->User_Project`.`ProjectId` AS `Projects.User_Project.ProjectId`
  9. FROM `Users` AS `User`
  10. LEFT OUTER JOIN `User_Projects` AS `Projects->User_Project` ON
  11. `User`.`id` = `Projects->User_Project`.`UserId`
  12. LEFT OUTER JOIN `Projects` AS `Projects` ON
  13. `Projects`.`id` = `Projects->User_Project`.`ProjectId` AND
  14. `Projects->User_Project`.`completed` = 1;

Including everything

To include all associated models, you can use the all and nested options:

  1. // Fetch all models associated with User
  2. User.findAll({ include: { all: true }});
  3. // Fetch all models associated with User and their nested associations (recursively)
  4. User.findAll({ include: { all: true, nested: true }});

Including soft deleted records

In case you want to eager load soft deleted records you can do that by setting include.paranoid to false:

  1. User.findAll({
  2. include: [{
  3. model: Tool,
  4. as: 'Instruments',
  5. where: { size: { [Op.ne]: 'small' } },
  6. paranoid: false
  7. }]
  8. });

Ordering eager loaded associations

When you want to apply ORDER clauses to eager loaded models, you must use the top-level order option with augmented arrays, starting with the specification of the nested model you want to sort.

This is better understood with examples.

  1. Company.findAll({
  2. include: Division,
  3. order: [
  4. // We start the order array with the model we want to sort
  5. [Division, 'name', 'ASC']
  6. ]
  7. });
  8. Company.findAll({
  9. include: Division,
  10. order: [
  11. [Division, 'name', 'DESC']
  12. ]
  13. });
  14. Company.findAll({
  15. // If the include uses an alias...
  16. include: { model: Division, as: 'Div' },
  17. order: [
  18. // ...we use the same syntax from the include
  19. // in the beginning of the order array
  20. [{ model: Division, as: 'Div' }, 'name', 'DESC']
  21. ]
  22. });
  23. Company.findAll({
  24. // If we have includes nested in several levels...
  25. include: {
  26. model: Division,
  27. include: Department
  28. },
  29. order: [
  30. // ... we replicate the include chain of interest
  31. // at the beginning of the order array
  32. [Division, Department, 'name', 'DESC']
  33. ]
  34. });

In the case of many-to-many relationships, you are also able to sort by attributes in the through table. For example, assuming we have a Many-to-Many relationship between Division and Department whose junction model is DepartmentDivision, you can do:

  1. Company.findAll({
  2. include: {
  3. model: Division,
  4. include: Department
  5. },
  6. order: [
  7. [Division, DepartmentDivision, 'name', 'ASC']
  8. ]
  9. });

In all the above examples, you have noticed that the order option is used at the top-level. The only situation in which order also works inside the include option is when separate: true is used. In that case, the usage is as follows:

  1. // This only works for `separate: true` (which in turn
  2. // only works for has-many relationships).
  3. User.findAll({
  4. include: {
  5. model: Post,
  6. separate: true,
  7. order: [
  8. ['createdAt', 'DESC']
  9. ]
  10. }
  11. });

Complex ordering involving sub-queries

Take a look at the guide on sub-queries for an example of how to use a sub-query to assist a more complex ordering.

Nested eager loading

You can use nested eager loading to load all related models of a related model:

  1. const users = await User.findAll({
  2. include: {
  3. model: Tool,
  4. as: 'Instruments',
  5. include: {
  6. model: Teacher,
  7. include: [ /* etc */ ]
  8. }
  9. }
  10. });
  11. console.log(JSON.stringify(users, null, 2));

Output:

  1. [{
  2. "name": "John Doe",
  3. "id": 1,
  4. "Instruments": [{ // 1:M and N:M association
  5. "name": "Scissor",
  6. "id": 1,
  7. "userId": 1,
  8. "Teacher": { // 1:1 association
  9. "name": "Jimi Hendrix"
  10. }
  11. }]
  12. }]

This will produce an outer join. However, a where clause on a related model will create an inner join and return only the instances that have matching sub-models. To return all parent instances, you should add required: false.

  1. User.findAll({
  2. include: [{
  3. model: Tool,
  4. as: 'Instruments',
  5. include: [{
  6. model: Teacher,
  7. where: {
  8. school: "Woodstock Music School"
  9. },
  10. required: false
  11. }]
  12. }]
  13. });

The query above will return all users, and all their instruments, but only those teachers associated with Woodstock Music School.

Using findAndCountAll with includes

The findAndCountAll utility function supports includes. Only the includes that are marked as required will be considered in count. For example, if you want to find and count all users who have a profile:

  1. User.findAndCountAll({
  2. include: [
  3. { model: Profile, required: true }
  4. ],
  5. limit: 3
  6. });

Because the include for Profile has required set it will result in an inner join, and only the users who have a profile will be counted. If we remove required from the include, both users with and without profiles will be counted. Adding a where clause to the include automatically makes it required:

  1. User.findAndCountAll({
  2. include: [
  3. { model: Profile, where: { active: true } }
  4. ],
  5. limit: 3
  6. });

The query above will only count users who have an active profile, because required is implicitly set to true when you add a where clause to the include.