sequelize: update method tries to insert null when receiving undefined

What you are doing?

I map some request to Sequelize model :

        var model = {
            title: req.body.title,
            description: req.body.description
        };
        models.job.update(model, {..}).then(() => {})

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

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 8
  • Comments: 21 (14 by maintainers)

Most upvoted comments

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 🙂

reproduced on sequelize v4 as well

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.

@gabegorelick , that is not correct.

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.

This actually supports my claim…

Workaround is to set the fields option on the update function to Object.keys(req.body), worked for me, at least.

This is still an issue on v4