Support updating/deleting with associations in a single step
See original GitHub issueIssue Description
Note: I am creating this issue to centralize a topic that often appears in different issues.
Is your feature request related to a problem? Please describe.
Similarly to the possibility of running a single create
call to create associated object together with the main object, users often want to update / delete an object with associated objects as well, such as in #7703, #9678 and https://github.com/RobinBuschmann/sequelize-typescript/issues/309. I am opening this issue to track all this in a single place.
Currently, our docs clarify that this is not possible.
In contrast, performing updates and deletions involving nested objects is currently not possible. For that, you will have to perform each separate action explicitly.
Describe the solution you’d like
I don’t have a clear concrete idea in mind. In fact, I think this is the biggest challenge for implementing this - to decide how exactly this will work. See this comment on #6808 and this comment on #5471.
Why should this be in Sequelize
This is a complex task that would be great to have covered by the ORM itself.
Describe alternatives/workarounds you’ve considered
The alternative/workaround is to do every create/update/delete by hand on a case-by-case basis…
Additional context
Very related issues: #6808 and #5471 Other related issues: #9199 #9233 #7703 #11699
Issue Template Checklist
Is this issue dialect-specific?
- No. This issue is relevant to Sequelize as a whole.
- Yes. This issue only applies to the following dialect(s): XXX, YYY, ZZZ
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don’t know how to start, I would need guidance.
- No, I don’t have the time, although I believe I could do it if I had the time…
- No, I don’t have the time and I wouldn’t even know how to start.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:95
- Comments:16 (2 by maintainers)
Hi @papb , I’m a long time Ruby on Rails developer and therefore I have some input regarding this comment: https://github.com/sequelize/sequelize/issues/6808#issuecomment-525883974
ActiveRecord (RoR’s ORM) goes by this rule of thumb:
primaryKey
of that record, it will create a new record.primaryKey
of that record, it will update the record.primaryKey
and the_destroy
key has a truthy value, ActiveRecord will destroy the record.Again with JSON examples:
Assuming all primary keys in this example are
id
.Hope it gives some direction towards bringing this functionality to Sequelize.
I implemented the fillAndSave method in my base class of my models to help me in these cases. So I can do things like:
Model:
I hope this helps in something … 😉🙏