You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
destroy() in paranoid mode does not persist properties updated in a beforeDestroy hook #9318
I'm opening a pull request for this in a minute, but I wanted to raise the issue first.
What are you doing?
I have a model in paranoid mode. This model gets a beforeDestroy hook attached in order to update a property on deletion (updatedBy which is a FK to another model, but this is not related to the problem described here).
When calling destroy(), instances are correctly soft deleted by my changed property is not persisted.
There's no way for Sequelize to know that you want to save anything unless you tell it to in the hook. Values don't get bubbled up to the destroy function, that would cause unexpected behaviour. @mickhansen
Other hooks like beforeCreate / beforeUpdate can modify instance and modified value will be save, I would say beforeDestroy should allow that too when dealing with paranoid models which is internally updating records.
@sushantdhiman I feel like that could have unintended consequences unless it's really made clear in the docs what the discrepancy in behaviours between different hook types are.
Paranoid is turned on and has been working. Because I have a uniqueness constraint on names, I want to modify the names of soft-deleted items, to release the name back into the pool (so to speak). Appending the deletion timestamp is a pretty common practice, and that's what I'm attempting to do.
When I run the destroy call, I see the log statement, I see the SQL update statement that modifies the name, but the soft-delete SQL never executes.
When I comment out the update call, soft-deletion works normally, but the name is obviously not updated.
I think your advice is not going to work for people.
In my specific case, I was able to get good results by making this an afterDestroy hook instead. I hope that helps someone.
Wouldn't calling save() or update() trigger beforeUpdate() ? If so , does it happen synchronously, that is, is the instance deleted only after beforeUpdate has completed execution ?
If not, I think @tomprogers 's case would be justified.
Activity
fix(destroy): Properties updated in a beforeDestroy hook are now pers…
heisian commentedon Apr 19, 2018
This is pretty simple - you need to call
saveorupdate.Your
beforeDestroyhook should actually look like this:...or this:
There's no way for Sequelize to know that you want to save anything unless you tell it to in the hook. Values don't get bubbled up to the destroy function, that would cause unexpected behaviour.
@mickhansen
sushantdhiman commentedon Apr 19, 2018
Other hooks like
beforeCreate / beforeUpdatecan modify instance and modified value will be save, I would saybeforeDestroyshould allow that too when dealing with paranoid models which is internally updating records.fix(destroy): Properties updated in a beforeDestroy hook are now pers…
fix(destroy): Properties updated in a beforeDestroy hook are now pers…
heisian commentedon Apr 19, 2018
@sushantdhiman I feel like that could have unintended consequences unless it's really made clear in the docs what the discrepancy in behaviours between different hook types are.
tomprogers commentedon May 22, 2018
@heisian The official documentation states very explicitly that you can make arbitrary changes to the model, and they will be persisted:
The example code below that text very strongly suggests that simple assignment of new values to model attributes is sufficient.
If callers must manually invoke save/update within the hook (in some or all cases), then the documentation needs to be corrected.
tomprogers commentedon May 23, 2018
@heisian I should add that performing save/update on the entity being destroyed prevents it from being destroyed.
My hook:
My destroy call:
Paranoid is turned on and has been working. Because I have a uniqueness constraint on names, I want to modify the names of soft-deleted items, to release the name back into the pool (so to speak). Appending the deletion timestamp is a pretty common practice, and that's what I'm attempting to do.
When I run the destroy call, I see the log statement, I see the SQL update statement that modifies the name, but the soft-delete SQL never executes.
When I comment out the update call, soft-deletion works normally, but the name is obviously not updated.
I think your advice is not going to work for people.
In my specific case, I was able to get good results by making this an
afterDestroyhook instead. I hope that helps someone.danedavid commentedon Jun 27, 2018
Wouldn't calling
save()orupdate()triggerbeforeUpdate()? If so , does it happen synchronously, that is, is the instance deleted only afterbeforeUpdatehas completed execution ?If not, I think @tomprogers 's case would be justified.
fix(destroy): Properties updated in a beforeDestroy hook are now pers…
fix(destroy): Properties updated in a beforeDestroy hook are now pers…