Recursive include Sequelize?

sequelize include
sequelize include where child
sequelize create include
sub include sequelize
multilevel include sequelize
sequelize associations
sequelize include through

I have category that can have child categories

And when I'm doing findAll I want to include all of those nested, but I don't know the depth.

var includeCondition = { 
                         include: [
                               model: models.categories,
                               as:'subcategory', nested: true 

        .then(function (categories) {
        .catch(function (err) {

The result brings me only one level nested include.



            // no subcategory here            

Can I somehow make sequalize include those nested subcategories ?

There are few solutions if found for this first one is more complicated but will give better performance:

This one is about implementing hierarchical data structure in MySQL I like the guide here

The one that is named The Nested Set Model.

The second solution that I actually implemented by myself is recursive expanding, this one uses lots of mysql requests and I believe can be improved, but it's a fast one and works well. The thing is to use for each category function like this

var expandSubcategories = function (category) {
    return new promise(function (resolve, reject) {
        category.getSubcategories().then(function (subcategories) {
            //if has subcategories expand recursively inner subcategories
            if (subcategories && subcategories.length > 0) {
                var expandPromises = [];
                _.each(subcategories, function (subcategory) {

                promise.all(expandPromises).then(function (expandedCategories) {
                    category.subcategories = [];

                    _.each(expandedCategories, function (expandedCategory) {
                    }, this);

                    //return self with expanded inner

            } else {
                //if has no subcategories return self

So it's going through the categories and expanding them recursively.

Maybe this will help someone as well.

sequelize-hierarchy, Need private packages and team management tools?Check out npm Teams ». sequelize-hierarchy. 2.0.4 • Public • Published a year ago. Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

There is a node module which handle it : sequelize-hierarchy It adds column parentId and hierarchyLevel to your table. As an example, this is what I did to order employees skills in a tree. Skills could be "Macro" -> "Excel" -> "Office" -> "Computer" database.js:

const  Sequelize = require('sequelize');
const sequelize = new Sequelize("stackoverflow", null, null, {
  dialect: "sqlite",
  storage: "database.db"
sequelize.sync().then(() => {console.log("Database ready");});
module.exports = sequelize;


module.exports = (sequelize, DataTypes) => {
  const Skill = sequelize.define("skill", {
    name:           DataTypes.STRING,
  return Skill;

Then in your controller:

Skill.findAll().then(skills => {
  res.send(skills); // Return a list
Skill.findAll({ hierarchy: true }).then(skills => {
  res.send(skills); // Return a tree

Infinite loop using recursive models · Issue #466 · RobinBuschmann , Hello, I'm using Sequelize (version -m5.0.0-beta.9) + Postgresql I'm having So I suspect the scope + include to be the source of the problem. Sequelize currently has no support for common table expressions and recursive CTEs. Adding the ability to include a CTE into the find* family of methods would allow find* to perform recursive queries. To see some examples of situations w

Suppose you have 5 different models A, B, C, D, E and A is associated with B, B with C and so on. So while fetching data for A you can get the all the nested subcategory hierarchy by using

include: [{ all: true, nested: true }]

Example: A.findAll(where:{// add conditions}, { include: [{ all: true, nested: true }]});

Please implement eager loading of nested associations · Issue #388 , In the code below, Variant.find can include Product, but what I'd like to be able In the meantime, I'm using the excellent sequelize.query to achieve the They named "recursive" the property that controls how deep the finder  'Include' in Sequelize: The One Confusing Query That You Should Memorize. When querying your database in Sequelize, you'll often want data associated with a particular model which isn't in the model's table directly.

Recursive include Sequelize?, Recursive include Sequelize? Question. I have category that can have child categories. And when I'm doing findAll I want to include  Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication and more. Sequelize follows SEMVER. Supports Node v6 and above to use ES6 features. Sequelize v5 was released on March 13, 2019.

Scopes, It is also possible to include scoped models in a scope definition. This allows Includes are merged recursively based on the models being included. This is a  This plugin for Sequelize solves this problem. Current status. API is stable. All features and options are fairly well tested. Works with all dialects of SQL supported by Sequelize (MySQL, Postgres, SQLite) except for Microsoft SQL Server. Requires Sequelize v2.x.x, v3.x.x, v4.x.x or v5.x.x. Supports only Node v8 or higher. Usage Loading module

Eager Loading, In Sequelize, eager loading is mainly done by using the include option on a all models associated with User and their nested associations (recursively) User. Defaults are inherited from sequelize.options.hierarchy if defined in call to new Sequelize(). Aliases for relations. as: Name of parent association. Defaults to 'parent' childrenAs: Name of children association. Defaults to 'children' ancestorsAs: Name of ancestors association. Defaults to 'ancestors' descendentsAs: Name of descendents association.