Skip to content

Commit

Permalink
Don't update virtual attributes in Model.update. Fixes #2860
Browse files Browse the repository at this point in the history
  • Loading branch information
janmeier committed Apr 13, 2015
1 parent 34da9ce commit 2606346
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Next
- [BUG] Don't update virtual attributes in Model.update. Fixes [#2860](https://github.com/sequelize/sequelize/issues/2860)
- [BUG] Fix for newlines in hstore [#3383](https://github.com/sequelize/sequelize/issues/3383)
- [BUG] Fix unique key handling in Model.update [#3474](https://github.com/sequelize/sequelize/issues/3474)
- [BUG] Fix issue with Model.create() using fields not specifying and non-incremental primary key [#3458](https://github.com/sequelize/sequelize/issues/3458)
Expand Down
12 changes: 4 additions & 8 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,8 @@ module.exports = (function() {
delete values[key];
}
});
} else {
options.fields = Object.keys(this.tableAttributes);
}

if (this._timestampAttributes.updatedAt) {
Expand Down Expand Up @@ -1722,14 +1724,8 @@ module.exports = (function() {
return [results.length, results];
}

Object.keys(valuesUse).forEach(function (attr) {
if (self.rawAttributes[attr].field && self.rawAttributes[attr].field !== attr) {
valuesUse[self.rawAttributes[attr].field] = valuesUse[attr];
delete valuesUse[attr];
}
});

Utils.mapOptionFieldNames(options, self);
valuesUse = Utils.mapValueFieldNames(valuesUse, options.fields, self);
options = Utils.mapOptionFieldNames(options, self);

// Run query to update all rows
return self.QueryInterface.bulkUpdate(self.getTableName(options), valuesUse, options.where, options, self.tableAttributes).then(function(affectedRows) {
Expand Down
24 changes: 24 additions & 0 deletions test/integration/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,30 @@ describe(Support.getTestDialectTeaser('Model'), function() {
});
});

it('does not update virtual attributes', function () {
var User = this.sequelize.define('User', {
username: Sequelize.STRING,
virtual: Sequelize.VIRTUAL
});

return User.create({
username: 'jan'
}).then(function () {
return User.update({
username: 'kurt',
virtual: 'test'
}, {
where: {
username: 'jan'
}
});
}).then(function () {
return User.findAll();
}).spread(function (user) {
expect(user.username).to.equal('kurt');
});
});

it('sets updatedAt to the current timestamp', function() {
var data = [{ username: 'Peter', secretValue: '42' },
{ username: 'Paul', secretValue: '42' },
Expand Down

0 comments on commit 2606346

Please sign in to comment.