Attributes modified in hooks are not saved
See original GitHub issueIf you add a hook to (for exemple) a User
model, and save an instance of the model with some attributes modified, such as:
user.email = 'toto@toto.com';
user.save();
And if you modify some user attributes into the (for exemple…) afterValidate
hook:
function afterUserValidate(user) {
if(user.changed('email')) {
return u.md5(user.email).then((md5) => user.emailMd5 = md5);
}
}
The emailMd5
attribute is not saved. This is because dirty attributes are computed before the pre-save hooks.
So my question: could we imagine to compute dirty attributes after the pre-save hooks?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:6
- Comments:41 (12 by maintainers)
Top Results From Across the Web
React hooks not updating components - Stack Overflow
It's not updating your component because react doesn't think your state has changed from the previous render as you're modifying your state ...
Read more >How to Use Formidable Hooks
frm_images_dropdown_option_classes: This hook allows to modify the CSS classes of each option in FrmAppHelper::images_dropdown() method.
Read more >useFieldArray - Simple React forms validation
Custom hook for working with Field Arrays (dynamic form). ... Name of the attribute with autogenerated identifier to use as the key prop....
Read more >Mongoose v6.8.1: Middleware
pre('updateOne', { document: true, query: false }) . Note: The create() function fires save() hooks. Note: Query middlewares are not executed on subdocuments....
Read more >Operation hooks | LoopBack Documentation
Using operation hooks enables you to intercept actions that modify data ... NOTE: When findOrCreate finds an existing model, the save hooks are...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
After digging further, for anyone else who ends up here, it is by design that the beforeUpdate (including beforeSave, on an existing record) is NOT allowed to change additional attributes that were not previously changed before the update/save call.
You can see the code in the current master branch starting here: https://github.com/sequelize/sequelize/blob/bb30483a95658bc1e729d5ae2a966f86872f955d/src/model.js#L4108
It collects a list of attributes in which it will ignore changes (ignoreChanged), which is all the configured attributes not already changed when the update/save is called. It uses that list to filter out any additional changes made during the execution of the hook.
You can override this behavior by manually adding the changed fields to options.fields before your hook function returns.
This bug still exists in 4.22.12. If you set an attribute in a beforeUpdate hook that was not part of the original list of update fields, the updated field appears in the response but not in the database. You can’t call .save in a beforeUpdate hook b/c it creates an endless loop.