Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2847,8 +2847,8 @@ class Model {

options.type = QueryTypes.BULKUPDATE;

// Clone values so it doesn't get modified for caller scope
values = _.clone(values);
// Clone values so it doesn't get modified for caller scope and ignore undefined values
values = _.omitBy(values, value => value === undefined);

// Remove values that are not in the options.fields
if (options.fields && options.fields instanceof Array) {
Expand Down Expand Up @@ -3913,6 +3913,9 @@ class Model {
* @return {Promise<this>}
*/
update(values, options) {
// Clone values so it doesn't get modified for caller scope and ignore undefined values
values = _.omitBy(values, value => value === undefined);

const changedBefore = this.changed() || [];

options = options || {};
Expand Down
10 changes: 10 additions & 0 deletions test/integration/instance/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,16 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
});
});

it('ignores undefined attributes', function() {
return this.User.sync({force: true}).bind(this).then(() => {
return this.User.create({ username: 'user' }).then(user => {
return user.update({ username: undefined }).then(user => {
expect(user.username).to.equal('user');
});
});
});
});

it('doesn\'t update primary keys or timestamps', function() {
const User = this.sequelize.define('User' + config.rand(), {
name: DataTypes.STRING,
Expand Down
20 changes: 20 additions & 0 deletions test/integration/model/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}));
});

it('should ignore undefined values without throwing not null validation', function() {
const ownerId = 2;
return this.Account.create({
ownerId,
name: Math.random().toString()
}).then(account => {
return this.Account.update({
name: Math.random().toString(),
ownerId: undefined
}, {
where: {
id: account.get('id')
}
});
}).then(() => {
return this.Account.findOne();
}).then(account => {
expect(account.ownerId).to.be.equal(ownerId);
});
});

if (_.get(current.dialect.supports, 'returnValues.returning')) {
it('should return the updated record', function() {
Expand Down