Skip to content

setting field to undefined then saving it causes ER_EMPTY_QUERY#3317

@sjlu

Description

@sjlu

Using Sequelize 2.0.4

At the moment, when I change a field by nulling it out, it will clear out that field appropriately. An example of this is:

User.find(id).then(function(user){user.firstname = null; return user.save()}); 

On the other hand, when I undefine it, for example:

user.firstname = undefined; user.save() 

It will throw me SequelizeDatabaseError: ER_EMPTY_QUERY: Query was empty since it is the only field that has changed. Note that on my model I have timestamps: false.

Executing (default):{[SequelizeDatabaseError: ER_EMPTY_QUERY: Query was empty] name: 'SequelizeDatabaseError', message: 'ER_EMPTY_QUERY: Query was empty', parent:{[Error: ER_EMPTY_QUERY: Query was empty] code: 'ER_EMPTY_QUERY', errno: 1065, sqlState: '42000', index: 0, sql: '' }, original:{[Error: ER_EMPTY_QUERY: Query was empty] code: 'ER_EMPTY_QUERY', errno: 1065, sqlState: '42000', index: 0, sql: '' }, sql: '' } 

Now when diving into the code a little bit more, I see that it is picking up that the field has changed when logging options on https://github.com/sequelize/sequelize/blob/master/lib/instance.js#L581

But when it hits Utils.mapValueFieldNames, the value has disappeared due to it interpreting the value as undefined and therefore not picking it up on https://github.com/sequelize/sequelize/blob/master/lib/utils.js#L230

I can understand that null has a philosophically different meaning than undefined where undefined in Sequelize means "don't touch this value", should it be treated as null in this case? I know that it kind of duplicates specifying which fields to save https://github.com/sequelize/sequelize/blob/master/lib/instance.js#L541

If yes, should the call end early by adding

if (!_.keys(values).length) return this; 

Or no and should it interpret undefined as null?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions