-
Notifications
You must be signed in to change notification settings - Fork 2
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
Comments
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. |
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:
Do this:
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. |
Hi Weston,
Thank you for your response. I needed the solution quickly. I checked other
ORM libraries and switched to Objection.js. They implemented so called
Upsert function which considers also the relationships. It updates values
on the main entity, removes relationships in join table when the related
item is missing in JSON object passed to this function. It also inserts new
related items (present in JSON, having no ID value). I know it is very
complex task. I was used to this in Hibernate where it works very nicely.
What I wanted was, that I just update complex entity including its
relations on frontend, then I pass the adequate JSOn object to backend and
the backend app (rest API) uses ORM framework to do the whole job - without
need to implement all the checks and logic on my own.
As I mentioned I don't need this anymore in Sequelize, but it might be good
to include it for the future.
Regards
Michal
…On Mon, Dec 24, 2018 at 3:08 AM Weston Siegenthaler < ***@***.***> wrote:
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.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#7 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ArVca4DrLoyRrG7ilwAGREvHUhr6-niPks5u8DcYgaJpZM4Y4lzh>
.
|
Thanks for responding.
My team is pretty committed to Sequelize and I like your sequelize-embed
library. It just so happened that the activity that
led me to your library first was a BelongsToMany association. It might make
sense to have a main mode that 1.) does not interact with any
join table attibutes other than keys, and then 2.) have an option that
sets/gets join table attributes also.
Thanks
Brian
…On Sun, Dec 23, 2018 at 9:08 PM Weston Siegenthaler < ***@***.***> wrote:
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.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#7 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AFXVW75JUgHvIa2foQZNIFnHetPTmIawks5u8DcYgaJpZM4Y4lzh>
.
--
*Brian E. Moore, PE*
Moore Technical Services, LLC
[email protected]
www.mooretechnicalservices.com
812-322-6563
|
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.
The text was updated successfully, but these errors were encountered: