39

How to perform nested include ? I have table products that has one to many relation with comments, and table comments has many to one relation with users table. So comments has user_id, and product_id. My code is like this

var models = require('../models');

models.products.findAll({
    include: [
        {model: models.comments}
    ]
  }).then(function (products) {
    next(products);
  }).catch(function (err) {
    next(err);
  });
});

I get the comments, but I would like to have something like

models.products.findAll({
    include: [
        {model: models.comments.include(models.comments.users)}
    ]
  }) 

Is this possible without writing custom queries ?

5 Answers 5

54
models.products.findAll({
  include: [
    {model: models.comments, include: [models.comments.users] }
  ]
}) 
3
  • 4
    please add some explanation to your answer Nov 26, 2015 at 19:45
  • 14
    This solution does not work as written (at least not in the version I have, 3.24.1). The inner include should be 'include: [models.users]'. Sep 2, 2016 at 20:42
  • Be carefull if this is a many-to-many association. In this case you should use hasMany when define association.
    – dalmate
    Sep 22 at 4:07
11

Solution provided didn't work for me, this is the typescript version I use and guessing the sequelize versions:

// sequelize-typescript
models.products.findAll({
  where,
  include: [{
    model: Comment,
    include: [User]
  }]
});

// without typescript (guessing here)
models.products.findAll({
  where,
  include: [{
    model: models.comments,
    include: [{
      model: models.users
    }]
  }]
});

1
  • This seems to compile, but I have an error on my side : column X->Y.link dos not exist. But I have in X : HasMany Y and in Y I have BelongsTo X. I don't understand why I can't include this way in typescript sequelize
    – Alexy
    Jul 13, 2021 at 17:16
4

There is a very simple and concise way! https://sequelize.org/docs/v6/advanced-association-concepts/eager-loading/#including-everything

// Fetch all models associated with User
User.findAll({ include: { all: true }});

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

In case this helps someone in the future:

I was nesting multiple levels deep, and I kept getting Typescript errors that my call didn't match any overrides.

For me, it was not clear in the previous answers (or the docs) that you must wrap the include object in an array at all levels (even in you only provide 1 object).

Works:

User.findAll({
  include: [{
    model: Contact,
    include: [{
      model: Address,
      include: [{
        model: City,
        include: [State, Country]
      }]
    }]
  }]
});

Does NOT Work:

User.findAll({
  include: { // <-- notice the missing square bracket
    model: Contact,
    include: { // <-- notice the missing square bracket
      model: Address,
      include: { // <-- notice the missing square bracket
        model: City,
        include: [State, Country]
      } // <-- notice the missing square bracket
    } // <-- notice the missing square bracket
  } // <-- notice the missing square bracket
});
-5

You can use include.

For example :

Category = sequelize.define(...);
Product = sequelize.define(...);
Product.belongsTo(Category, {foreignKey: 'fk_category'});

Product.findAll({
    include: [{
        model: Category
    }]
});

Will retrieve the nested Category, and not just the id, in a category property.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.