belongsToMany relation in another belongsToMany relationship using Sequelize

76 views
Skip to first unread message

Kartikeya Mishra

unread,
Nov 15, 2017, 7:44:29 AM11/15/17
to Sequelize

I was going through document of sequelize. In my database I had user and roles having n:m relations. http://docs.sequelizejs.com/manual/tutorial/associations.html#belongs-to-many-associations

User.hasMany(Teams, {through: UserTeams});

Teams.hasMany(User, {through: UserTeams});

This creates another table UserTeams. Now UserTeams has m:n relation with Styles. Therefore the relations will be defined as:

UserTeams.hasMany({Styles, {through: UserTeamStyles});

Styles.hasMany({UserTeams, {through: UserTeamStyles});

User.findAll({     
     include: [{
         model: Teams,
         through: {
           attributes: ['a','b','c']
         }
     }]
});

This will give me data=> User left join with UserTeams and inner join with Teams. How can I get the data of UserTeamStyles and styles along with this data in a single query?

Reply all
Reply to author
Forward
0 new messages