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
update method tries to insert null when receiving undefined #7056
let say that req.body.title is undefined and req.body.description has value.
In the model, title cant be null.
For some reason sequelize tries to insert null to the title although it is undefined, resulting in getting the following error :
err.stack SequelizeValidationError: notNull Violation: title cannot be null
this happens probably because model.hasOwnProperty('title') returns true, but yet, model.title is undefined and not null
What do you expect to happen?
I think that in that case, when a field is undefined, Sequelize should not try to insert null into it (the title field) and just ignore it
What is actually happening?
i get an error :
err.stack SequelizeValidationError: notNull Violation: title cannot be null
I think we should insert null only when someone specifically specify that this field is null.
Dialect: mysql
__Database version: 5.7.9
__Sequelize version: 3.28.0
Reacted by Rovack, Damian Bouné, Dino Bettini, Vladyslav Huba, Taemin Cha, Raymond Bernardo, Jeremy Kelly, miikalei and Nguyen Huu Phuc
I've done few more testing and I found out that if the field allowed to have nulls, and i try to update with value undefined it actually skips it as desired.
This actually supports my claim.. but it gives a different approach, maybe the problem is only with the validation ?
If it is skipped, then why the validation checks for undefined as well : (file ./lib/instance-validator.js)
if(rawAttribute.allowNull===false&&((value===null)||(value===undefined))){error=newsequelizeError.ValidationErrorItem(field+' cannot be null','notNull Violation',field,value);this.errors.push(error);}
could be
if(rawAttribute.allowNull===false&&value===null){error=newsequelizeError.ValidationErrorItem(field+' cannot be null','notNull Violation',field,value);this.errors.push(error);}
further more, if i remove that validation, everything works as magic. sequelize doesnt try to insert null/undefined to the field and just skips it.
so this issue is relevant only for not null fields. for others it works correctly.
that's true the only way to keep a value is to reinsert this sounds weird for an update especially for people who are used to update just the field they want . @mickhansen your input is appreciated.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
Reacted by Matthieu Harlé, swyx.io, jony89, Michael Donahue, Rovack, writebeat, Dino Bettini, Indospace.io, Albert Folch, Akinwunmi Oluwaseun and 2 more
This behavior is fairly consistent throughout Sequelize; e.g.
Model.findAll({where: {foo: undefined}});
will run SELECT ... WHERE foo IS NULL. I personally find this inconvenient as it means I need to remove undefined values from my where options, but if that's the decision Sequelize has made then present-but-undefined values should be treated as NULL everywhere. Otherwise, it would probably require a major version bump to change this behavior.
As I shown above, there is different behaviour between nullable props and non-nullable props.
I've done few more testing and I found out that if the field allowed to have nulls, and i try to update with value undefined it actually skips it as desired.
Until this is fixed there is no way to update fields in a record without also setting all of the non-null field values, even if those are not changing.
I've created a PR for this a while ago : #7167
I'm pretty sure this is the correct solution, yet it was for v3 and the tests did not completely pass, need to give it some more time. I'll try to redo it this weekend.
Here you go. I have a User model with field 'name' on it:
module.exports=(sequelize,types)=>{constUser=sequelize.define('user',{// some fields...name: {type: types.STRING,allowNull: false,validate: {notEmpty: {msg: 'Name cannot be empty'}}},// some other fields...},/* some options... */)returnUser}
I import it to Sequelize. I have an api route to edit a user. It looks like this:
exports.editMe=async(req,res)=>{constuser=awaitUser.findById(/* ... */)console.log(user.get())// {... , name: 'Kek!', ...}// assume that req.body is empty object herereq.body.name=undefined// if I uncomment next line, it works as expected, not trying to insert null// delete req.body.nameconsole.log(req.body)// {name: undefined}awaituser.update(req.body)// SequelizeValidationError: notNull Violation: user.name cannot be null}
Activity
jony89 commentedon Jan 24, 2017
if i add :
at ./lib/models.js after the comment :
it works well, is this a possible solution ? should i create a PR?
jony89 commentedon Jan 30, 2017
I've done few more testing and I found out that if the field allowed to have nulls, and i try to update with value undefined it actually skips it as desired.
This actually supports my claim.. but it gives a different approach, maybe the problem is only with the validation ?
If it is skipped, then why the validation checks for undefined as well : (file ./lib/instance-validator.js)
could be
further more, if i remove that validation, everything works as magic. sequelize doesnt try to insert null/undefined to the field and just skips it.
so this issue is relevant only for not null fields. for others it works correctly.
agalazis commentedon Mar 14, 2017
that's true the only way to keep a value is to reinsert this sounds weird for an update especially for people who are used to update just the field they want . @mickhansen your input is appreciated.
jony89 commentedon May 30, 2017
reproduced on sequelize v4 as well
stale commentedon Jul 29, 2017
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂
jony89 commentedon Jul 31, 2017
this issue still need to be fixed. @mickhansen
jony89 commentedon Sep 30, 2017
please reopen this, still need to be resolved.
sushantdhiman commentedon Sep 30, 2017
I assume can be reproduced on master
jony89 commentedon Sep 30, 2017
Didn't retest it for a while, but yeah
gabegorelick commentedon Oct 17, 2017
This behavior is fairly consistent throughout Sequelize; e.g.
will run
SELECT ... WHERE foo IS NULL. I personally find this inconvenient as it means I need to remove undefined values from my where options, but if that's the decision Sequelize has made then present-but-undefinedvalues should be treated as NULL everywhere. Otherwise, it would probably require a major version bump to change this behavior.jony89 commentedon Oct 23, 2017
@gabegorelick , that is not correct.
As I shown above, there is different behaviour between nullable props and non-nullable props.
millermatt commentedon Feb 23, 2018
Until this is fixed there is no way to update fields in a record without also setting all of the non-null field values, even if those are not changing.
writebeat commentedon May 29, 2018
This is still an issue on v4
writebeat commentedon May 29, 2018
Workaround is to set the
fieldsoption on the update function toObject.keys(req.body), worked for me, at least.gabegorelick commentedon Jun 20, 2018
May be resolved by #9548.
Alexsey commentedon Jun 20, 2018
No, it would not be resolved with #9548 because that one is only about
undefinedinwhereand here we haveundefinedin update objectjony89 commentedon Jun 21, 2018
I've created a PR for this a while ago : #7167
I'm pretty sure this is the correct solution, yet it was for v3 and the tests did not completely pass, need to give it some more time. I'll try to redo it this weekend.
1valdis commentedon Sep 16, 2018
This is still an issue for me on v4.38.1. When we'd expect a fix?
jony89 commentedon Sep 16, 2018
Can u share some code ? This should be fixed. it is tested manual+unit tests.
1valdis commentedon Sep 16, 2018
Here you go. I have a User model with field 'name' on it:
I
importit to Sequelize. I have an api route to edit a user. It looks like this:jony89 commentedon Sep 16, 2018
Yeah that's the case.
I am sorry but it seems it was merged only to master (version > 5)
I asked to merged it to v3 as well and got negative answer :\
You can find the PR here #9587
So either upgrade or can also use (as i mentioned be4)