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

Commit ea5afbfd by Wei-Liang Liou Committed by Sushant

fix(model): map fields only once when saving (#10658)

1 parent 33e140d9
Showing with 67 additions and 1 deletions
...@@ -3784,7 +3784,6 @@ class Model { ...@@ -3784,7 +3784,6 @@ class Model {
args = [this, this.constructor.getTableName(options), values, options]; args = [this, this.constructor.getTableName(options), values, options];
} else { } else {
where = this.where(true); where = this.where(true);
where = Utils.mapValueFieldNames(where, Object.keys(where), this.constructor);
if (versionAttr) { if (versionAttr) {
values[versionFieldName] = parseInt(values[versionFieldName], 10) + 1; values[versionFieldName] = parseInt(values[versionFieldName], 10) + 1;
} }
......
...@@ -813,6 +813,40 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -813,6 +813,40 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}); });
}); });
describe('save', () => {
it('should mapping the correct fields when saving instance. see #10589', function() {
const User = this.sequelize.define('User', {
id3: {
field: 'id',
type: Sequelize.INTEGER,
primaryKey: true
},
id: {
field: 'id2',
type: Sequelize.INTEGER,
allowNull: false
},
id2: {
field: 'id3',
type: Sequelize.INTEGER,
allowNull: false
}
});
// Setup
return this.sequelize.sync({ force: true }).then(() => {
return User.create({ id3: 94, id: 87, id2: 943 });
})
// Test
.then(() => User.findByPk(94))
.then(user => user.set('id2', 8877))
.then(user => user.save({ id2: 8877 }))
// Validate
.then(() => User.findByPk(94))
.then(user => expect(user.id2).to.equal(8877));
});
});
describe('update', () => { describe('update', () => {
it('throws an error if no where clause is given', function() { it('throws an error if no where clause is given', function() {
const User = this.sequelize.define('User', { username: DataTypes.STRING }); const User = this.sequelize.define('User', { username: DataTypes.STRING });
...@@ -827,6 +861,39 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -827,6 +861,39 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}); });
}); });
it('should mapping the correct fields when updating instance. see #10589', function() {
const User = this.sequelize.define('User', {
id3: {
field: 'id',
type: Sequelize.INTEGER,
primaryKey: true
},
id: {
field: 'id2',
type: Sequelize.INTEGER,
allowNull: false
},
id2: {
field: 'id3',
type: Sequelize.INTEGER,
allowNull: false
}
});
// Setup
return this.sequelize.sync({ force: true }).then(() => {
return User.create({ id3: 94, id: 87, id2: 943 });
})
// Test
.then(() => User.findByPk(94))
.then(user => {
return user.update({ id2: 8877 });
})
// Validate
.then(() => User.findByPk(94))
.then(user => expect(user.id2).to.equal(8877));
});
if (current.dialect.supports.transactions) { if (current.dialect.supports.transactions) {
it('supports transactions', function() { it('supports transactions', function() {
return Support.prepareTransactionTest(this.sequelize).then(sequelize => { return Support.prepareTransactionTest(this.sequelize).then(sequelize => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!