You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
how to define the relations between 3 tables #6671
Reacted by Vladyslav Huba, LenoM, Sarang N, Ibrahima Dansoko, Winston Gao, Aswin Sanakan, Mar Castanon, AnotherFullstackDev, Andrey Guzyuk, Pedro Mendez Y. and 22 more
@felixfbecker This way makes one role only belongs to one case, but I need case:role relations is n:n, and role:user relations is n:n too, help me please.
@shuperry I would suggest that you build 3 sets of 1:n relations between the join table and the other three. Your queries will be complex, but your scenario is complex as well. so:
Define a model: CaseRoleUser
Add three primary key attributes: case_id, role_id, user_id
Associate the tables via their corresponding foreign key with belongsTo and hasMany.
@shuperry It is not clear to me how your tables are associated, an ER diagram would help. But as @alitaheri all types of associations are supported in some way. If you still need help, please ask on StackOverflow on Slack.
Reacted by Naveen S, Pedro Mendez Y., Odravison 'Odra' Junior, Mihai Plasoianu, Arsham Mohammadi Neyshabori, DenysPoliarush, Marco Kerwitz, Viney, baboo35 and Oleksandr Prypkhan
@felixfbecker OP wants to define what roles a given user has on a given case. For a given user - case association there should be not just one role, but arbitrarily many roles. In other words, a "many-to-many-to-many" association.
I'm not a database expert but it does seem to me that in pure SQL, a single table with three foreign keys is the simplest solution for this problem. For instance in this table user Jim has read and write access to case 07A, and user Bob has read-only access to cases 23F and 24Y.
case_id
user_id
role_id
07A
Jim
read
07A
Jim
write
23F
Bob
read
24Y
Bob
read
Unfortunately it doesn't seem to me like Sequelize provides any conceptual API for a table with more than two foreign key columns?
@shuperry (apologies for writing this before I read how you were nesting your associations, my example is a bit different...) The only solution I can think of at the moment is to split this three-way relation into two separate BelongsToMany relations: make Users and Cases have a belongsToMany association through a CaseUsers table, and the CaseUsers join table in turn has a belongsToMany association with Roles. Here is how the same relations as above would be expressed with this association strategy:
@felixfbecker I realized this morning, my proposed solution to the OP's problem doesn't work. It seems to me there is really no viable way to have a many-to-many-to-many relationship in Sequelize.
@jedwards1211 Our project already finished, but still thank you, at the end time, I figured out the way you said too, this way may be the simplest way, I just did't say at here, and there is no need to rewrite the code.
@alfredomp The best way is write model associate as jedwards1211 said, but the select is unimplementable, you need to do the complex select and write the structure you need.
@shuperry after reading the above mentioned comments, it is still not clear to me whether you got a way(solution) to do association using SEQUELIZE, for 3 tables in 1 junction table.
I have been facing difficulties doing this and I have the same requirement to implement many-to-many-to-many relation in one table.
I realized that they are proposing to do A-to-B in C and then use composite key of from C (A+B) then do C-to-D in E.
It would be helpful if you can tell me whether or not you achieved A-B-C as foreign keys in D(junction table), and if not what do you think as the best way to achieve a near-to solution.
Can you help me with sharing the structure, considering example,Table 1 is USERS, Table 2 is GROUPS, and Table 3 is PERMISSIONS to be joined in USER_TO_GROUP_PERMISSION Table 4.
@shuperry after reading the above mentioned comments, it is still not clear to me whether you got a way(solution) to do association using SEQUELIZE, for 3 tables in 1 junction table.
I have been facing difficulties doing this and I have the same requirement to implement many-to-many-to-many relation in one table.
I realized that they are proposing to do A-to-B in C and then use composite key of from C (A+B) then do C-to-D in E.
It would be helpful if you can tell me whether or not you achieved A-B-C as foreign keys in D(junction table), and if not what do you think as the best way to achieve a near-to solution.
Can you help me with sharing the structure, considering example,Table 1 is USERS, Table 2 is GROUPS, and Table 3 is PERMISSIONS to be joined in USER_TO_GROUP_PERMISSION Table 4.
Activity
felixfbecker commentedon Oct 8, 2016
All documented here: http://docs.sequelizejs.com/en/v3/docs/associations/#belongs-to-many-associations
shuperry commentedon Oct 8, 2016
@felixfbecker This is 2 tables, I know this, but mine is 3 tables, I want to join 3 table with 1 table
shuperry commentedon Oct 8, 2016
@felixfbecker This way makes one role only belongs to one case, but I need case:role relations is n:n, and role:user relations is n:n too, help me please.
alitaheri commentedon Oct 8, 2016
@shuperry I would suggest that you build 3 sets of 1:n relations between the join table and the other three. Your queries will be complex, but your scenario is complex as well. so:
CaseRoleUsercase_id,role_id,user_idbelongsToandhasMany.felixfbecker commentedon Oct 8, 2016
@shuperry It is not clear to me how your tables are associated, an ER diagram would help. But as @alitaheri all types of associations are supported in some way. If you still need help, please ask on StackOverflow on Slack.
shuperry commentedon Oct 8, 2016
@alitaheri Thanks first, this way can created right table, and now I only need to write sequelize model.findAll() data construct myself.
alitaheri commentedon Oct 8, 2016
Well the queries will be very complex, but with include you should be able to do what you need:
shuperry commentedon Oct 9, 2016
@alitaheri I know this way, but the structure is not my need, I need Role include User, no matter, I can write the structure myself.
jedwards1211 commentedon Feb 5, 2018
@felixfbecker OP wants to define what roles a given user has on a given case. For a given user - case association there should be not just one role, but arbitrarily many roles. In other words, a "many-to-many-to-many" association.
I'm not a database expert but it does seem to me that in pure SQL, a single table with three foreign keys is the simplest solution for this problem. For instance in this table user
Jimhas read and write access to case07A, and userBobhas read-only access to cases23Fand24Y.case_iduser_idrole_idUnfortunately it doesn't seem to me like Sequelize provides any conceptual API for a table with more than two foreign key columns?
@shuperry (apologies for writing this before I read how you were nesting your associations, my example is a bit different...) The only solution I can think of at the moment is to split this three-way relation into two separate
BelongsToManyrelations: makeUsers andCases have abelongsToManyassociation through aCaseUsers table, and theCaseUsers join table in turn has abelongsToManyassociation withRoles. Here is how the same relations as above would be expressed with this association strategy:EDIT: this solution doesn't work!
CaseUseridcase_iduser_idCaseUserRolecase_user_idrole_idjedwards1211 commentedon Feb 6, 2018
@felixfbecker I realized this morning, my proposed solution to the OP's problem doesn't work. It seems to me there is really no viable way to have a many-to-many-to-many relationship in Sequelize.
shuperry commentedon Feb 7, 2018
@jedwards1211 Our project already finished, but still thank you, at the end time, I figured out the way you said too, this way may be the simplest way, I just did't say at here, and there is no need to rewrite the code.
alfredomp commentedon Feb 26, 2018
@shuperry how did you end up solving this issue? I am having the same situation.
shuperry commentedon Feb 27, 2018
@alfredomp The best way is write model associate as
jedwards1211said, but the select is unimplementable, you need to do the complex select and write the structure you need.achintya03 commentedon May 6, 2020
@shuperry after reading the above mentioned comments, it is still not clear to me whether you got a way(solution) to do association using SEQUELIZE, for 3 tables in 1 junction table.
I have been facing difficulties doing this and I have the same requirement to implement many-to-many-to-many relation in one table.
After reading docs of Sequelize
https://sequelize.org/master/manual/advanced-many-to-many.html#many-to-many-to-many-relationships-and-beyond
I realized that they are proposing to do A-to-B in C and then use composite key of from C (A+B) then do C-to-D in E.
It would be helpful if you can tell me whether or not you achieved A-B-C as foreign keys in D(junction table), and if not what do you think as the best way to achieve a near-to solution.
Can you help me with sharing the structure, considering example,Table 1 is USERS, Table 2 is GROUPS, and Table 3 is PERMISSIONS to be joined in USER_TO_GROUP_PERMISSION Table 4.
task-115: fix: определить N:N:N-отношение
task-115: feat: описать модели для функционала форума
feat: альфа-версия для демо
Hapi-swaggerstony commentedon Oct 14, 2025
https://honourclimatic109.github.io/I7CihGgvI68U/