-
Notifications
You must be signed in to change notification settings - Fork 127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple hasAndBelongsToMany() relations on the same model don't work #189
Comments
Arg, I have to admit that I never thought of this case when I designed I'm not quite sure what a good work-around is for now. We definitively need to add information in the link table (in your example I can think of a two solutions:
That way we can also put
I kind of prefer the second relation because I can make it backward compatible. Implementing the first solution would require people to rename their table (as for now since you can't simply rename a table, you have to copy all its content). @mindjuice -- again thank you for the feedback. This is really helping, especially in this case where I really never thought about the case of having different |
For the second solution, if you do not provide the last argument, we just keep the current behavior. If you "tag" your relation, we'll use it in the table name. |
The second solution seems better to me. In the second solution, where would the tag come into play? As a new field in the
Or did you have something else in mind? |
What a perfect timing, I am also facing the same issue and I tend to prefer the second solution as well. The field should probably named 'type' which would type the relation between those 2 entities. |
Thanks @tlvenn for bringing up the naming. Names are very important to get right. To me at least, 'type' implies an overall group of similar things that may or may not be related. But 'tag' implies more of a name (like a name tag). In this case, it's the name of a relation, and any two relations that have the same tag refer to the two sides of the same relation. On the other hand, tag has a well known meaning in HTML/XML. It's all related to how to perform the join as well, so perhaps being more explicit is an option too. Other possibilities: joinTag, joinOn, joinName, relationName I'm okay with 'tag', but I'm open to a more explicit name if that's preferred (I like Apple's Objective-C naming style too though, so there's that!). |
Err I wrote a long answer, but somehow GitHub (or Chromium) ate it :/ So now the syntax for
options can be The
|
This is released in 1.16.5 :) |
Thanks for the fix! One thing though, the table it's creating is called "Class_UserClass_User_student". I think on line 683 of model.js you only need either |
Arg good catch @mindjuice! Just released 1.16.6 with the fix :) Thanks again! |
I've patched line 683 locally and can confirm that multiple relations are working for me now! |
I have a
Class
model defined as follows:I see that this creates a
Class_User
table with fields forClass_id
andUser_id
(andid
) and when I save a new entry in thestudents
array,Class_User
gets updated with a new document.However, when I later
getJoin({teachers: true, students: true})
I get the same set of users in bothteachers
andstudents
.To make multiple relations on the same models work, I think you need to include the name of the relation field in the two ids:
Class_teachers_id
Class_students_id
Then, of course, you would need to change the
getJoin()
code to lookup the right relation based on the field name. Does that make sense?Or perhaps simpler, include the name of the relation as a prefix to the id string and keep the field names as is:
Class_id: teachers-ddb49718-9f0d-464a-9a7f-d4c7ec131e18
Class_id: students-a3895a71-98e5-4089-bbec-d26a226c4993
I hope I'm not being too much of a bother with filing all these issues, but I really like RethinkDB and Thinky so far and want to use them for a long time and help make them better!
The text was updated successfully, but these errors were encountered: