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

Commit 81c33d8c by Anton Vynogradenko Committed by Sushant

fix(model): generate ON CONFLICT ... DO UPDATE correctly (#11666) (#11744)

1 parent 6c781d66
...@@ -2703,7 +2703,7 @@ class Model { ...@@ -2703,7 +2703,7 @@ class Model {
// Get primary keys for postgres to enable updateOnDuplicate // Get primary keys for postgres to enable updateOnDuplicate
options.upsertKeys = _.chain(model.primaryKeys).values().map('field').value(); options.upsertKeys = _.chain(model.primaryKeys).values().map('field').value();
if (Object.keys(model.uniqueKeys).length > 0) { if (Object.keys(model.uniqueKeys).length > 0) {
options.upsertKeys = _.chain(model.uniqueKeys).values().filter(c => c.fields.length === 1).map('column').value(); options.upsertKeys = _.chain(model.uniqueKeys).values().filter(c => c.fields.length === 1).map(c => c.fields[0]).value();
} }
} }
......
...@@ -542,8 +542,53 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -542,8 +542,53 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}); });
}); });
}); });
it('when the primary key column names and model field names are different and have unique constraints', function() {
const Person = this.sequelize.define('Person', {
emailAddress: {
type: DataTypes.STRING,
allowNull: false,
primaryKey: true,
unique: true,
field: 'email_address'
},
name: {
type: DataTypes.STRING,
allowNull: false,
field: 'name'
}
}, {});
return Person.sync({ force: true })
.then(() => {
const inserts = [
{ emailAddress: 'a@example.com', name: 'Alice' }
];
return Person.bulkCreate(inserts);
})
.then(people => {
expect(people.length).to.equal(1);
expect(people[0].emailAddress).to.equal('a@example.com');
expect(people[0].name).to.equal('Alice');
const updates = [
{ emailAddress: 'a@example.com', name: 'CHANGED NAME' },
{ emailAddress: 'b@example.com', name: 'Bob' }
];
return Person.bulkCreate(updates, { updateOnDuplicate: ['emailAddress', 'name'] });
})
.then(people => {
expect(people.length).to.equal(2);
expect(people[0].emailAddress).to.equal('a@example.com');
expect(people[0].name).to.equal('CHANGED NAME');
expect(people[1].emailAddress).to.equal('b@example.com');
expect(people[1].name).to.equal('Bob');
});
});
}); });
it('should reject for non array updateOnDuplicate option', function() { it('should reject for non array updateOnDuplicate option', function() {
const data = [ const data = [
{ uniqueName: 'Peter', secretValue: '42' }, { 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!