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

Commit 3a600693 by PeledYuval Committed by Sushant

fix(bulkCreate): use correct primary key column name for updateOnDuplicate (#11434)

1 parent 5047bda1
......@@ -2700,7 +2700,7 @@ class Model {
if (options.updateOnDuplicate) {
options.updateOnDuplicate = options.updateOnDuplicate.map(attr => model.rawAttributes[attr].field || attr);
// Get primary keys for postgres to enable updateOnDuplicate
options.upsertKeys = _.chain(model.primaryKeys).values().map('fieldName').value();
options.upsertKeys = _.chain(model.primaryKeys).values().map('field').value();
if (Object.keys(model.uniqueKeys).length > 0) {
options.upsertKeys = _.chain(model.uniqueKeys).values().filter(c => c.fields.length === 1).map('column').value();
}
......
......@@ -34,6 +34,16 @@ describe(Support.getTestDialectTeaser('Model'), () => {
no: { type: DataTypes.INTEGER, primaryKey: true },
name: { type: DataTypes.STRING, allowNull: false }
});
this.Car = this.sequelize.define('Car', {
plateNumber: {
type: DataTypes.STRING,
primaryKey: true,
field: 'plate_number'
},
color: {
type: DataTypes.TEXT
}
});
return this.sequelize.sync({ force: true });
});
......@@ -480,7 +490,8 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
it('should support the updateOnDuplicate option with primary keys', function() {
describe('should support the updateOnDuplicate option with primary keys', () => {
it('when the primary key column names and model field names are the same', function() {
const data = [
{ no: 1, name: 'Peter' },
{ no: 2, name: 'Paul' }
......@@ -506,6 +517,33 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
});
it('when the primary key column names and model field names are different', function() {
const data = [
{ plateNumber: 'abc', color: 'Grey' },
{ plateNumber: 'def', color: 'White' }
];
return this.Car.bulkCreate(data, { fields: ['plateNumber', 'color'], updateOnDuplicate: ['color'] }).then(() => {
const new_data = [
{ plateNumber: 'abc', color: 'Red' },
{ plateNumber: 'def', color: 'Green' },
{ plateNumber: 'ghi', color: 'Blue' }
];
return this.Car.bulkCreate(new_data, { fields: ['plateNumber', 'color'], updateOnDuplicate: ['color'] }).then(() => {
return this.Car.findAll({ order: ['plateNumber'] }).then(cars => {
expect(cars.length).to.equal(3);
expect(cars[0].plateNumber).to.equal('abc');
expect(cars[0].color).to.equal('Red');
expect(cars[1].plateNumber).to.equal('def');
expect(cars[1].color).to.equal('Green');
expect(cars[2].plateNumber).to.equal('ghi');
expect(cars[2].color).to.equal('Blue');
});
});
});
});
});
it('should reject for non array updateOnDuplicate option', function() {
const data = [
{ uniqueName: 'Peter', secretValue: '42' },
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!