不要怂,就是干,撸起袖子干!

Commit 7a49a658 by jony89 Committed by Sushant

fix(model): ignore undefined values in update payload (#9587)

1 parent bb12f8d5
...@@ -2847,8 +2847,8 @@ class Model { ...@@ -2847,8 +2847,8 @@ class Model {
options.type = QueryTypes.BULKUPDATE; options.type = QueryTypes.BULKUPDATE;
// Clone values so it doesn't get modified for caller scope // Clone values so it doesn't get modified for caller scope and ignore undefined values
values = _.clone(values); values = _.omitBy(values, value => value === undefined);
// Remove values that are not in the options.fields // Remove values that are not in the options.fields
if (options.fields && options.fields instanceof Array) { if (options.fields && options.fields instanceof Array) {
...@@ -3913,6 +3913,9 @@ class Model { ...@@ -3913,6 +3913,9 @@ class Model {
* @return {Promise<this>} * @return {Promise<this>}
*/ */
update(values, options) { 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() || []; const changedBefore = this.changed() || [];
options = options || {}; options = options || {};
......
...@@ -374,6 +374,16 @@ describe(Support.getTestDialectTeaser('Instance'), () => { ...@@ -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() { it('doesn\'t update primary keys or timestamps', function() {
const User = this.sequelize.define('User' + config.rand(), { const User = this.sequelize.define('User' + config.rand(), {
name: DataTypes.STRING, name: DataTypes.STRING,
......
...@@ -35,6 +35,26 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -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')) { if (_.get(current.dialect.supports, 'returnValues.returning')) {
it('should return the updated record', function() { it('should return the updated record', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!