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

Commit 9e78d786 by Mick Hansen

fix(model): provide .field support for .update

1 parent c42f6b7e
Showing with 33 additions and 0 deletions
......@@ -1640,6 +1640,15 @@ 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];
}
});
mapFieldNames.call(self, options, self);
// Run query to update all rows
return self.QueryInterface.bulkUpdate(self.getTableName(), valuesUse, options.where, options, self.tableAttributes).then(function(affectedRows) {
if (options.returning) {
......
......@@ -222,6 +222,30 @@ describe(Support.getTestDialectTeaser("Model"), function () {
});
});
it('should bulk update', function () {
var Entity = this.sequelize.define('Entity', {
strField: {type: Sequelize.STRING, field: 'str_field'},
});
return this.sequelize.sync({force: true}).then(function() {
return Entity.create({strField: 'foo'});
}).then(function() {
return Entity.update(
{strField: 'bar'},
{where: {strField: 'foo'}}
);
}).then(function () {
return Entity.findOne({
where: {
strField: 'bar'
}
}).then(function (entity) {
expect(entity).to.be.ok;
expect(entity.get('strField')).to.equal('bar');
});
});
});
it('should not contain the field properties after create', function () {
var Model = this.sequelize.define('test', {
id: {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!