sequelize: instance.destroy(...) does not return any information if rows has been deleted or not
using Sequelize 3.3.2
var user = User.build({ id: 1 }, { isNewRecord: false })
user.destroy()
.then(function(){
console.log(arguments)
})
Code above always prints { '0': [] }
no matter if row has been deleted or not. I am not sure if this is intentional, but it would useful to know that… Also returning
option could be very useful (postgres) as I’ve mentioned in #4122
About this issue
- Original URL
- State: open
- Created 9 years ago
- Reactions: 1
- Comments: 36 (22 by maintainers)
@mickhansen @janmeier
instance.destroy()
still returns empty array in postgresql. Any updates on this and why is it closed?Any updates on this?
This doesn’t warrant opening a separate issue. But I have a question. The docs state destroy returns a Promise. That’s fine, but what does the Promise contain? I wish that was added into the docs.
@kmlbdh Keep in mind that the maintainers of this library are working on it for what is basically free. If you really need this feature, you could open a PR and/or help review an existing one instead of making comments that are demotivating for the people that put in the work to improve this library.
In fact instance.save() also does not provide informations if record has been actually updated. Also I was wondering why
Model.destroy
(bulk destroy) returns(rows)
from promise whileModel.update
returns([rows])
(in array) from promise. To sum up:till 2022, there is no solution for this issue! I think working with this library is wasting time!
I am using v4.37.5 and
Model.destroy()
still returns an empty array. However the document says:Not sure if I missed anything?
I’ve started working on this in PR #4299. No tests yet. So far I’ve managed to make it work like this:
One thing I can’t figure out is how to deal with
bulkUpdate
andbulkDestroy
and hooks. It is possible to passindividualHooks
option to both methods and bothbeforeUpdate/Destroy
andafterUpdate/Destroy
hooks will be executed for each instance. However, since there isreturning
option, it would be useful to able to performbulkUpdate/bulkDestroy
WITHOUT individualbefore
hooks, but WITHIN individualafter
hooks. What do you think guys?Also I was wondering if it wouldn’t be useful to pass results of bulkUpdate and bulkCreate (
[rows, instances]
) toafterBulkUpdate
andafterBulkDelete
hooks:I noticed this problem when investigating another issue, I was already aware of this but didn’t know there was an issue for it!
Hmm, the problem is even deeper I guess. When you call ANY instance method that affects databse (instance.save, instance.update, instance.destroy) you NEVER know if that rows still exists in db or not… Instance is just server side description of row that is not actually synced with database - so in @mickhansen example above theoretically even if
update
failed, it does not mean that row does not exists when it comes toinstance.addAssociation()
.