Repository navigation
Description
Issue 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 sequelize/sequelize-typescript#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.
Activity
Nitemaeric commentedon Jan 29, 2020
Hi @papb , I'm a long time Ruby on Rails developer and therefore I have some input regarding this comment: #6808 (comment)
ActiveRecord (RoR's ORM) goes by this rule of thumb:
primaryKeyof that record, it will create a new record.primaryKeyof that record, it will update the record.primaryKeyand the_destroykey 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.
sergiomendonca commentedon Jul 8, 2020
Up!
wakeupmh commentedon Dec 3, 2020
Does this remain unsolved?
filipesarturi commentedon Dec 6, 2020
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 ... 😉🙏
LeslieLei commentedon Jan 14, 2021
how can i implement this
klausXR commentedon Apr 9, 2021
Plus one for this functionality. I believe TypeORM and Mikro-Orm both support this and I feel like it is rather hard to manage write operations without this functionality.
Also, a lot of the ideas for the Repository pattern from DDD and similar methodologies sort of rely on this - its not impossible without it, but it involves much more code.
Also, I don't know if the documentation for association methods is written poorly, but I couldn't get the
setmethods to work for associations besidesbelongsToMany.For example, for One to many relationships, I would like to do
User.setAddresses([ Model, Model ]), where the model is an instance build withAddress.build(data).I'd like the ones that I passed to be persisted and anything else to be unassociated, but the set method does not work like that.
I frequently get myself into similar situations, which are harder to handle without the aforementioned functionality.
I create a
Userwith two addresses.Then, later on, somebody tries to update the user with 2 new addresses ( the two old ones must be unassociated )
The
setmethods expects me to have "created" instances, but in order to create it without the association, I would need to have an user address withnullcolumn foraddress.userId.If it worked with models and I was able to save it in one go, like in the other more popular orms, it would be much easier to handle.
versus whatever I have to do in Sequelize to achieve the same functionality ( I couldn't figure it out )
microvn commentedon May 2, 2021
up
sun-slaven commentedon Aug 3, 2021
not resolved yet?
joaquinodz commentedon Aug 23, 2021
Quick Fix:
const attributes = this.constructor.attributes->const attributes = this.constructor.tableAttributesI think it was modified at some point ¯_(ツ)_/¯
aclarembeau commentedon Sep 30, 2021
Personally, I ended up writing my own utility function:
It's probably not as flexible/complete as the other solution in this thread. But, as it's simpler, it can serve as a basis if someone wants to extend it :)
k0dsubs commentedon Nov 2, 2021
Hi, can you explain on how to use your function, thx in advance, I've tried:
let user = await User.findByPk(req.params.id, {include: Address});
await user.set(req.body) // this includes array with addresses;
user = await deepSave(user) //your function,
await user.save(); //albeith your function does that
n4an commentedon Nov 18, 2021
How to use it without that? The most important part is miss
WikiRik commentedon Dec 18, 2021
@mittche I think your comment is missing some information. What are you referring to with 'it' and 'that'?
nammon100x commentedon Apr 4, 2022
Has this been resolved?
erlinis commentedon Apr 29, 2022
This is a key feature, I need it !
ephys commentedon May 1, 2022
Feel free to open a PR for it
4ybakut2004 commentedon Oct 11, 2023
Hi! I'm currently switching from Ruby On Rails to NodeJS with sequelize. In sequelize without this feature i have to write a lot more code. This feature would be very useful for me too!
mustafa-patharia commentedon Jul 15, 2026
I've had to solve this in my own projects, and the logic that's worked well for me is pretty close to the Rails approach @Nitemaeric described. Sharing it in case it helps settle the semantics, since that seems to be the actual blocker here.
The idea: reuse the same
includeyou already pass tocreate(), but on the update/save path too. Then for each record in the payload:_destroy: true→ destroy itThe one rule I'd really push for: if something isn't in the payload, leave it alone. A missing field stays as-is, a child that's not in the array stays as-is, and an empty array means "no change", not "delete everything". Nothing gets removed unless it's explicitly marked
_destroy. That keeps partial updates safe — you should never wipe data just because it wasn't sent.Same rule for
belongsToMany: add the links that are new, leave the ones you didn't send, and only detach a link when it's marked_destroy(detach the join row by default, destroying the target could be opt-in). It should also let you set the through-table columns inline, which most of the workarounds in this thread skip.Everything runs in one transaction (wrap one if the caller didn't pass one), and it should handle non-
idprimary keys and run the normal validations on the nested writes too.If this direction sounds right I'm happy to build it — starting with one association type (probably
hasMany) so the PR stays reviewable, then extending. Mainly looking for a 👍 on the "absent = untouched, only_destroyremoves" rule before I start, since everything builds on that. Open to discussion on the rest.