Join across multiple junction tables with Sequelize

Assuming the following relations:

User.belongsToMany(Team, { through: 'users_teams'});
Team.belongsToMany(User, { through: 'users_teams'});

Folder.belongsToMany(Team, { through: 'teams_folders'});
Team.belongsToMany(Folder, { through: 'teams_folders'});

You should be able to load everything in one go using nested includes:

User.findAll({
  include: [
    {
      model: Team, 
      include: [
        Folder
      ]  
    }
  ]
});

You seem to be on the right track already with the example you have given in your post :). The only thing you need to change is instead of passing the User model directly in include, you pass an object with a model property and a further nested include property

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)