-
Notifications
You must be signed in to change notification settings - Fork 2k
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
When Many-to-Many Through Associations release? #5291
Comments
I am able to get this mostly working (not fully tested). It's a pretty manual job, and finnicky! For now, don't bother with attribute methods or lifecycle callbacks, as they'll get overwritten. I have an issue about that here: #707. Here's an example. I would love for someone to let me know if there's any easier way! In particular, I'm confused by the var Dogs= {
identity: 'dogs',
attributes: {
humanFriends: {
collection: 'humans',
through: 'friendships'
}
}
},
var Friendships = {
identity: 'friendships',
attributes: {
someOtherAttr: 'string',
dog: {
columnName: 'dog',
type: 'integer',
foreignKey: true,
references: 'dogs',
on: 'id',
onKey: 'id',
via: 'human'
},
human: {
columnName: 'human',
type: 'integer',
foreignKey: true,
references: 'humans',
on: 'id',
onKey: 'id',
via: 'dog'
}
}
}
var Humans = {
identity: 'humans',
attributes: {
dogFriends: {
collection: 'dogs',
through: 'friendships'
}
}
} |
Not working for me... |
@mewben, do you get that error right when waterline is initialized / when your sails lift (if you're using sails)? And which version of waterline are you using? I'd love to find a reproducible way to hack through-associations into working correctly! |
I'm using sails v0.10.5... sails lifts okay though.. the error triggers when using .add() in the association and then .save() to commit... |
@mewben using that exact example I gave you above, this is working for me. I'm on waterline 0.10.12 using sails-disk. Perhaps if you follow that error's stack trace, we can figure out if it's simply the version of waterline that you're running. |
I'm using the waterline v0.10.9 which sails v0.10.5 is using.. I guess I'll have to wait for sails to bump their waterline dependency then... |
I've tried a similar code with v0.10.12 and it worked for me, thanks @devinivy. |
The issue title did not correspond to the question in the issue body here.. Is there a date for the 'official' associations-attributes implementation? Or is it just the docs that are missing, and @devinivy has the right way to do it? |
I'm not sure about an official date/release– sadly it doesn't seem to be in the 0.11 milestone: https://github.com/balderdashy/waterline/milestones/0.11 . My method works, but it's undocumented. If you peek through the |
@NiclasLindqvist, the waterline-adapter-tests give a hint as to what may be the right way to describe many-to-many through associations in the future. Check hasManyThrough.venue.fixture.js (join table) and hasManyThrough.team.fixture.js. This approach works for joins (relevant test) but it seems to fail when creating an association. Using @devinivy's method does work for both creating associations and joins which makes me think that many-to-many through associations will be officially supported soon (fingers crossed). |
@devinivy what statement are you using to populate your join model once humans and dogs are populated? Also, is declaring the variables necessary? |
@kirkaleze declaring the variables is merely for illustrative purposes. The populate query would then look quite normal: But you should also be able to query the junction table: |
It would also be good to be able to store data in the join table (association) while saving. Quoting @elennaro (#957):
|
Yes, indeedy! Info on the join table should also be included appear when populating records during a |
Sorry for a noob question, but is there any manual way to overwrite find for humans somehow to get both Dogs and identity field from friendship? |
One of the things I'm really missing is the ability of pulling the join table record for each // joinRecord or some other keyword to enable this behaviour
Model.findOne(100).populate('assocAttribute', { joinRecord: true }) Would return something like: {
id: 100,
assocAttribute: [
{ id: 2, name: 'blah', createdAt: '04/05/2015', _joinRecord: { id: 10, createdAt: '05/05/2015', joinAttribute: 'something' } }
]
} What do you think about this API? Does anybody knows how other ORMs handle this? |
@dmarcelino that API looks similar to the suggested approach in Rails with the It would be amazing as part of the extension to the # To get all
Model.findOne(100).populate('assocAttribute', { joinRecord: true }) and # to get joins made since a date *criteria syntax is probably wrong
Model.findOne(100).populate('assocAttribute', { joinRecord: {where: {createdAt: {'>=': '04/04/2015'}}}}) and further to optimize for large join tables, one could also populate only the fields you were interested in maybe: # to get only id from join record
Model.findOne(100).populate('assocAttribute', { joinRecord: { select: ['id']}})
# to return
{
id: 100,
assocAttribute: [
{ id: 2, name: 'blah', createdAt: '04/05/2015', _joinRecord: { id: 10} }
]
} That all said, I'd be extremely happy to get the implementation you suggested for now 😄 👍 |
Anyone have a working example of this that they'd care to share? I can't seem to get this working properly. I'm sure I'm missing something quite simple. |
+1 Need this feature desperately. Did anyone manually implement it with success? |
👍 Please. |
I'm using 0.10.26 and all seems to work fine. I have a question thou how can you populate someOtherAttr in Friendship? is there a way to do it from one of the 2 tables part of the relationship or I do need to call explicitly Friendship? |
Hello. |
Thanks for posting, @cuongcua90. I'm a repo bot-- nice to meet you! It has been 30 days since there have been any updates or new comments on this page. If this issue has been resolved, feel free to disregard the rest of this message. On the other hand, if you are still waiting on a patch, please:
Thanks so much for your help! |
Reopening, as this is in a feature request in an active milestone. We can close once it's PRed into the roadmap. |
@devinivy Any ETA on when this will be available? Any patches we can integrate today? |
@mukk85 this is essentially usable– the configuration for it is just ugly and it's considered experimental. @atiertant might have a patch that you can integrate? |
@devinivy got some work on it,got something working a little,need more test...
like this this not possible to define if relation on taxi should use
|
@particlebanana merges patch to correct many bugs on this assocations type but not this one. |
Thanks for posting, @cuongcua90. I'm a repo bot-- nice to meet you! It has been 30 days since there have been any updates or new comments on this page. If this issue has been resolved, feel free to disregard the rest of this message. On the other hand, if you are still waiting on a patch, please:
Thanks so much for your help! |
Re-opening until this is added to the roadmap |
@particlebanana the real problem is we don't know what must be done... update doc? correct a bug ? add some tests,...just add a line in roadmap to say "something like this should work later" |
Ok this is on the Roadmap. Waterline is taking the contribution guide from Sails. So basically what needs to happen is the following:
This has a somewhat defined spec with the So TLDR: We have a loose spec of how to do M:M through with extra columns and it has been added to the roadmap. It could be scheduled for |
The docs said it is coming soon. while I wait it is released is the alternative way to attack addition attributes?
The text was updated successfully, but these errors were encountered: