Skip to content

Commit

Permalink
fix(model): map fields only once when saving (#10658)
Browse files Browse the repository at this point in the history
  • Loading branch information
ALiangLiang authored and sushantdhiman committed Mar 30, 2019
1 parent 33e140d commit ea5afbf
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
1 change: 0 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3784,7 +3784,6 @@ class Model {
args = [this, this.constructor.getTableName(options), values, options];
} else {
where = this.where(true);
where = Utils.mapValueFieldNames(where, Object.keys(where), this.constructor);
if (versionAttr) {
values[versionFieldName] = parseInt(values[versionFieldName], 10) + 1;
}
Expand Down
67 changes: 67 additions & 0 deletions test/integration/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
it('throws an error if no where clause is given', function() {
const User = this.sequelize.define('User', { username: DataTypes.STRING });
Expand All @@ -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) {
it('supports transactions', function() {
return Support.prepareTransactionTest(this.sequelize).then(sequelize => {
Expand Down

0 comments on commit ea5afbf

Please sign in to comment.