Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize hasOne through another table

I have the following three models

User

user_id
user_email

Group

group_id
group_name

GroupUser

group_user_id
user_id
group_id

How can I get group details(if it mapped with user ) while fetch User data ? Is there any Sequelize hasOne association through another table ?

like image 726
Kepler Avatar asked Nov 04 '25 23:11

Kepler


1 Answers

There is currently no direct way in sequelize for what you are asking

ref https://github.com/sequelize/sequelize/issues/3845

but you can do

Group.belongsTo(GroupUser)
User.belongsTo(GroupUser)
GroupUser.hasOne(Group)
GroupUser.hasOne(User)

and do nested include (but this is not very efficient)

User.findAll({include : [{model:GroupUser, include :[{model : Group}]}]})
like image 152
Keval Gohil Avatar answered Nov 07 '25 16:11

Keval Gohil