Skip to content
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

Update BelongsToMany associations (many-to-many through join table) #7

Open
endeva-tech opened this issue Nov 28, 2018 · 4 comments
Open

Comments

@endeva-tech
Copy link

Hello,

Am I correct in assumption that update of association defined using belongsToMany method is not implemented yet? I have checked the code of embed.js file and I can see function "updateBelongsTos" but not "updateBelongsToManys". This lead me to the assumption updating the many-to-many associations (through join table) are not possible yet. Please confirm. If so, is it planned?

Thanks for the information in advance.

@brianemoore57
Copy link

I am also interested in this. This is very nice work. It just so happens that the first code I tried is a many-to-many. I have stepped through with a debugger, and it finally dawned on me that its not going to process that association.

@Wsiegenthaler
Copy link
Owner

I wrote this library for another project which didn't need BelongsToMany and, considering it's the most complex of the association types, I never really got around to implementing it. It would be nice to have but I'd need to spend some time to think about it before making any promises.

What kind of semantics would you expect from such functionality? Should the call also insert, update, and/or delete records in the shared table, or just manage set membership via the join table? The latter case would be easier but might not be what you need.

If you need a solution sooner and only need to modify the join table, you can achieve such functionality today by modeling your schema a little differently. Instead of BelongsToMany you can explicitly model the join table and represent the relationship with a combination of HasMany and BelongsTo.

So instead of this:

Artist.belongsToMany(Genre)

Do this:

Artist.hasMany(ArtistGenre)
ArtistGenre.belongsTo(Genre)

I've used this strategy before and it does work with this library, the main caveats being that it adds an extra level of nesting to each element in the association and doesn't let you use the fancy BelongsToMany convenience methods provided by Sequelize.

Anyway, if you let me know a little more about your use case I can start thinking about adding it.

@endeva-tech
Copy link
Author

endeva-tech commented Dec 27, 2018 via email

@brianemoore57
Copy link

brianemoore57 commented Dec 28, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants