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

Commit e0340843 by Mick Hansen

fix(instance): update() should only save the provided attributes (leaving other …

…changes attributes alone), closes #2341
1 parent 82b7809b
Showing with 29 additions and 2 deletions
......@@ -638,7 +638,9 @@ module.exports = (function() {
// Ensure new values are on Instance, and reset previousDataValues
result.dataValues = _.extend(result.dataValues, values);
result._previousDataValues = _.clone(result.dataValues);
options.fields.forEach(function (field) {
result._previousDataValues[field] = result.dataValues[field];
});
}).tap(function(result) {
// Run after hook
if (options.hooks) {
......@@ -707,7 +709,14 @@ module.exports = (function() {
* @alias updateAttributes
*/
Instance.prototype.update = function(values, options) {
return this.set(values, {attributes: options && options.fields}).save(options);
options = options || {};
if (Array.isArray(options)) options = {fields: options};
this.set(values, {attributes: options.fields});
if (!options.fields) options.fields = _.intersection(Object.keys(values), this.changed());
return this.save(options);
};
Instance.prototype.updateAttributes = Instance.prototype.update;
......
......@@ -93,6 +93,24 @@ describe(Support.getTestDialectTeaser('Instance'), function() {
});
});
it('should only save passed attributes', function () {
var user = this.User.build();
return user.save().then(function () {
user.set('validateTest', 5);
expect(user.changed('validateTest')).to.be.ok;
return user.updateAttributes({
validateCustom: '1'
});
}).then(function () {
expect(user.changed('validateTest')).to.be.ok;
expect(user.validateTest).to.be.equal(5);
}).then(function () {
return user.reload();
}).then(function () {
expect(user.validateTest).to.not.be.equal(5);
});
});
it('should not set attributes that are not specified by fields', function () {
var User = this.sequelize.define('User' + config.rand(), {
name: DataTypes.STRING,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!