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

Commit fdf3e99e by Lars Tijhuis Committed by Sushant

fix(bulkCreate): error with updateOnDuplicate option (#8259)

1 parent 8a95e421
......@@ -2357,7 +2357,7 @@ class Model {
return this.QueryInterface.bulkInsert(this.getTableName(options), records, options, attributes).then(results => {
if (Array.isArray(results)) {
results.forEach((result, i) => {
instances[i].set(this.primaryKeyAttribute, result[this.rawAttributes[this.primaryKeyAttribute].field], {raw: true});
instances[i] && instances[i].set(this.primaryKeyAttribute, result[this.primaryKeyField], {raw: true});
});
}
return results;
......
......@@ -1760,12 +1760,15 @@ describe(Support.getTestDialectTeaser('Model'), () => {
if (dialect !== 'postgres' && dialect !== 'mssql') {
it('should support the ignoreDuplicates option', function() {
const self = this,
data = [{ uniqueName: 'Peter', secretValue: '42' },
{ uniqueName: 'Paul', secretValue: '23' }];
const self = this;
const data = [
{ uniqueName: 'Peter', secretValue: '42' },
{ uniqueName: 'Paul', secretValue: '23' }
];
return this.User.bulkCreate(data, { fields: ['uniqueName', 'secretValue'] }).then(() => {
data.push({ uniqueName: 'Michael', secretValue: '26' });
return self.User.bulkCreate(data, { fields: ['uniqueName', 'secretValue'], ignoreDuplicates: true }).then(() => {
return self.User.findAll({order: ['id']}).then(users => {
expect(users.length).to.equal(3);
......@@ -1781,9 +1784,11 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
} else {
it('should throw an error when the ignoreDuplicates option is passed', function() {
const self = this,
data = [{ uniqueName: 'Peter', secretValue: '42' },
{ uniqueName: 'Paul', secretValue: '23' }];
const self = this;
const data = [
{ uniqueName: 'Peter', secretValue: '42' },
{ uniqueName: 'Paul', secretValue: '23' }
];
return this.User.bulkCreate(data, { fields: ['uniqueName', 'secretValue'] }).then(() => {
data.push({ uniqueName: 'Michael', secretValue: '26' });
......@@ -1799,6 +1804,35 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
}
if (current.dialect.supports.updateOnDuplicate) {
it('should support the updateOnDuplicate option', function() {
const self = this;
const data = [
{ uniqueName: 'Peter', secretValue: '42' },
{ uniqueName: 'Paul', secretValue: '23' }
];
return this.User.bulkCreate(data, { fields: ['uniqueName', 'secretValue'], updateOnDuplicate: ['secretValue'] }).then(() => {
const new_data = [
{ uniqueName: 'Peter', secretValue: '43' },
{ uniqueName: 'Paul', secretValue: '24' },
{ uniqueName: 'Michael', secretValue: '26' }
];
return self.User.bulkCreate(new_data, { fields: ['uniqueName', 'secretValue'], updateOnDuplicate: ['secretValue'] }).then(() => {
return self.User.findAll({order: ['id']}).then(users => {
expect(users.length).to.equal(3);
expect(users[0].uniqueName).to.equal('Peter');
expect(users[0].secretValue).to.equal('43');
expect(users[1].uniqueName).to.equal('Paul');
expect(users[1].secretValue).to.equal('24');
expect(users[2].uniqueName).to.equal('Michael');
expect(users[2].secretValue).to.equal('26');
});
});
});
});
}
if (current.dialect.supports.returnValues) {
describe('return values', () => {
it('should make the autoincremented values available on the returned instances', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!