Repository navigation
Closed
Description
Issue Description
bulkCreate generates incorrect ON CONFLICT ... DO UPDATE clause when model has > 1 primary key field and column names differ from field names
What are you doing?
Here is a test case that illustrates the problem:
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: '[email protected]', name: 'Alice' }
];
return Person.bulkCreate(inserts);
})
.then(people => {
expect(people.length).to.equal(1);
expect(people[0].emailAddress).to.equal('[email protected]');
expect(people[0].name).to.equal('Alice');
const updates = [
{ emailAddress: '[email protected]', name: 'CHANGED NAME' },
{ emailAddress: '[email protected]', name: 'Bob' }
];
return Person.bulkCreate(updates, { updateOnDuplicate: ['emailAddress', 'name'] });
})
.then(people => {
expect(people.length).to.equal(2);
expect(people[0].emailAddress).to.equal('[email protected]');
expect(people[0].name).to.equal('CHANGED NAME');
expect(people[1].emailAddress).to.equal('[email protected]');
expect(people[1].name).to.equal('Bob');
});
});The test fails because model is generating the ON CONFLICT ... DO UPDATE clause using the name of the sequelize attribute (emailAddress), not the table's column name (email_address).
1) [POSTGRES] Model
bulkCreate
updateOnDuplicate
should support the updateOnDuplicate option with primary keys
RUNME when the primary key column names and model field names are different and have unique constraints:
SequelizeDatabaseError: column "emailAddress" does not exist
at Query.formatError (lib/dialects/postgres/query.js:366:16)
at /Users/huned/data/work/forks/sequelize-non-fork/lib/dialects/postgres/query.js:72:18
From previous event:
at Query.run (lib/dialects/postgres/query.js:64:23)
at /Users/huned/data/work/forks/sequelize-non-fork/lib/sequelize.js:644:29
From previous event:
at /Users/huned/data/work/forks/sequelize-non-fork/lib/sequelize.js:644:12
From previous event:
at /Users/huned/data/work/forks/sequelize-non-fork/lib/sequelize.js:640:10
at /Users/huned/data/work/forks/sequelize-non-fork/node_modules/retry-as-promised/index.js:70:21
at retryAsPromised (node_modules/retry-as-promised/index.js:60:10)
at /Users/huned/data/work/forks/sequelize-non-fork/lib/sequelize.js:630:30
From previous event:
at Sequelize.query (lib/sequelize.js:579:23)
at QueryInterface.bulkInsert (lib/query-interface.js:997:27)
at /Users/huned/data/work/forks/sequelize-non-fork/lib/model.js:2716:39
From previous event:
at /Users/huned/data/work/forks/sequelize-non-fork/lib/model.js:2667:12
From previous event:
at recursiveBulkCreate (lib/model.js:2616:10)
at Function.bulkCreate (lib/model.js:2845:12)
at /Users/huned/data/work/forks/sequelize-non-fork/test/integration/model/bulk-create.test.js:579:31
From previous event:
at Context.<anonymous> (test/integration/model/bulk-create.test.js:569:16)
at processImmediate (internal/timers.js:439:21)
postgres.log helpfully points out the problem:
2019-11-10 21:57:55.415 PST [51605] ERROR: column "emailAddress" does not exist at character 279
2019-11-10 21:57:55.415 PST [51605] HINT: Perhaps you meant to reference the column "People.email_address".
2019-11-10 21:57:55.415 PST [51605] STATEMENT: INSERT INTO "People" ("email_address","name","createdAt","updatedAt") VALUES ('[email protected]','CHANGED NAME','2019-11-11 05:57:55.412 +00:00','2019-11-11 05:57:55.412 +00:00'),('[email protected]','Bob','2019-11-11 05:57:55.412 +00:00','2019-11-11 05:57:55.412 +00:00') ON CONFLICT ("emailAddress") DO UPDATE SET "email_address"=EXCLUDED."email_address","name"=EXCLUDED."name" RETURNING "email_address","name","createdAt","updatedAt";
What do you expect to happen?
Generates ON CONFLICT ("email_address") DO UPDATE.
What is actually happening?
Generates ON CONFLICT ("emailAddress") DO UPDATE which is incorrect.
The problem is at lib/model.js:2706.
Additional context
N/A
Environment
- Sequelize version:
6.0.0-beta.3 - Node.js version:
v12.12.0 - Operating System: MacOS 10.15.1
- If TypeScript related: TypeScript version: N/A
Issue Template Checklist
How does this problem relate to dialects?
- I think this problem happens regardless of the dialect.
- I think this problem happens only for the following dialect(s): postgres (could be others - but I only know/use postgres)
- I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX
Would you be willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time and I know how to start.
- Yes, I have the time but I don't know how to start, I would need guidance.
- No, I don't have the time, although I believe I could do it if I had the time...
- No, I don't have the time and I wouldn't even know how to start.
Reacted by Alejandro Corredor and jaylattice
Metadata
Metadata
Assignees
Labels
No labels
Activity
huned commentedon Nov 12, 2019
Note: see also #11662
dsdavis4 commentedon Nov 19, 2019
Any status on this or #11662 being merged?