Repository navigation
Closed
Labels
Description
Issue Description
What are you doing?
I am trying to create entities in bulk with bulkCreate. In case of a conflict on the primary key, I want that entity to be updated with the new values I have given.
Model:
module.exports = (sequelize, DataTypes) => {
const Capability = sequelize.define(
'Capability', {
id: {
type: DataTypes.INTEGER,
field: 'id',
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING(255),
field: 'name',
allowNull: false
},
description: {
type: DataTypes.TEXT,
field: 'description',
allowNull: true
},
categoryId: {
type: DataTypes.INTEGER,
field: 'categoryId',
allowNull: false,
defaultValue: 1,
references: {
model: 'Category',
key: 'id'
},
onUpdate: 'NO ACTION',
onDelete: 'NO ACTION'
},
organizationId: {
type: DataTypes.INTEGER,
field: 'organizationId',
allowNull: false,
defaultValue: 18,
references: {
model: 'Organization',
key: 'id'
},
onUpdate: 'NO ACTION',
onDelete: 'NO ACTION'
},
deleted: {
type: DataTypes.BOOLEAN,
field: 'deleted',
allowNull: false,
defaultValue: false
},
updatedAt: {
type: DataTypes.DATE,
field: 'updatedAt',
allowNull: false
},
updatedById: {
type: DataTypes.INTEGER,
field: 'updatedById',
allowNull: false,
references: {
model: 'User',
key: 'id'
},
onUpdate: 'NO ACTION',
onDelete: 'NO ACTION'
},
createdAt: {
type: DataTypes.DATE,
field: 'createdAt',
allowNull: false
},
createdById: {
type: DataTypes.INTEGER,
field: 'createdById',
allowNull: false,
references: {
model: 'User',
key: 'id'
},
onUpdate: 'NO ACTION',
onDelete: 'NO ACTION'
},
capabilityStatusId: {
type: DataTypes.INTEGER,
field: 'capabilityStatusId',
allowNull: false,
defaultValue: 1,
references: {
model: 'CapabilityStatus',
key: 'id'
},
onUpdate: 'NO ACTION',
onDelete: 'NO ACTION'
},.....more.....
Controller:
console.log("fields=", fields);
console.log('createObjects= ', createObjects);
newObjects = await model.bulkCreate(createObjects, {
updateOnDuplicate: fields,
returning: true,
transaction: t
});
Here is the link to the SSCCE for this issue: LINK-HERE
// You can delete this code block if you have included a link to your SSCCE above!
// MINIMAL, SELF-CONTAINED code here (SSCCE/MCVE/reprex)What do you expect to happen?
I expect the generated sql statement will have ON CONFLICT(id) instead of ON CONFLICT ("categoryId", "updatedById", "createdById", "capabilityStatusId"), not sure why these four foreign keys would get to play here. If "cateorgyId" shows here, why not "organizationId"?
INSERT INTO
"public"."Capability" ("id", "name", "description", "categoryId", "organizationId", "deleted", "updatedAt", "updatedById", "createdAt", "createdById", "underAssessment", "capabilityStatusId", "riskScore", "acmmtName", "acmmtDescription", "completeFlag")
VALUES
(
1157, 'Capability 2(updated)', 'test1', 1, 183, false, '2021-03-02 17:51:29.384 +00:00', 204, '2021-03-02 17:51:29.384 +00:00', 204, false, 1, 0, '(xxx)', '(xxx)', false
)
,
(
1156, 'Capability 1(updated)', 'test2', 1, 183, false, '2021-03-02 17:51:29.384 +00:00', 204, '2021-03-02 17:51:29.384 +00:00', 204, false, 1, 0, '(xxx)', '(xxx)', false
)
ON CONFLICT ("categoryId", "updatedById", "createdById", "capabilityStatusId") DO
UPDATE
SET
"name" = EXCLUDED."name", "description" = EXCLUDED."description", "categoryId" = EXCLUDED."categoryId", "organizationId" = EXCLUDED."organizationId", "updatedById" = EXCLUDED."updatedById", "createdById" = EXCLUDED."createdById", "acmmtName" = EXCLUDED."acmmtName", "acmmtDescription" = EXCLUDED."acmmtDescription" RETURNING *;
What is actually happening?
fields= [
'createdById',
'updatedById',
'categoryId',
'acmmtName',
'acmmtDescription',
'organizationId',
'name',
'description'
]
createObjects= [
{
createdById: 204,
updatedById: 204,
categoryId: 1,
acmmtName: '(xxx)',
acmmtDescription: '(xxx)',
organizationId: 183,
id: 1157,
name: 'Capability 2(updated)',
description: 'test1'
},
{
createdById: 204,
updatedById: 204,
categoryId: 1,
acmmtName: '(xxx)',
acmmtDescription: '(xxx)',
organizationId: 183,
id: 1156,
name: 'Capability 1(updated)',
description: 'test2'
}
]
Executing (417a4075-f91c-47b9-b4ba-f65a3359c30f): INSERT INTO "public"."Capability" ("id","name","description","categoryId","organizationId","deleted","updatedAt","updatedById","createdAt","createdById","underAssessment","capabilityStatusId","riskScore","acmmtName","acmmtDescription","completeFlag") VALUES (1157,'Capability 2(updated)','test1',1,183,false,'2021-03-02 17:51:29.384 +00:00',204,'2021-03-02 17:51:29.384 +00:00',204,false,1,0,'(xxx)','(xxx)',false),(1156,'Capability 1(updated)','test2',1,183,false,'2021-03-02 17:51:29.384 +00:00',204,'2021-03-02 17:51:29.384 +00:00',204,false,1,0,'(xxx)','(xxx)',false) ON CONFLICT ("categoryId","updatedById","createdById","capabilityStatusId") DO UPDATE SET "name"=EXCLUDED."name","description"=EXCLUDED."description","categoryId"=EXCLUDED."categoryId","organizationId"=EXCLUDED."organizationId","updatedById"=EXCLUDED."updatedById","createdById"=EXCLUDED."createdById","acmmtName"=EXCLUDED."acmmtName","acmmtDescription"=EXCLUDED."acmmtDescription" RETURNING *;
Error in ingest transaction... DatabaseError [SequelizeDatabaseError]: there is no unique or exclusion constraint matching the ON CONFLICT specification
at Query.formatError (/Users/xxx/xxx/xxx/node_modules/sequelize/lib/dialects/postgres/query.js:366:16)
at /Users/xxx/xxx/xxx/node_modules/sequelize/lib/dialects/postgres/query.js:72:18
at tryCatcher (/Users/xxx/xxx/xxx/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/xxx/xxx/xxx/node_modules/bluebird/js/release/promise.js:547:31)
at Promise._settlePromise (/Users/xxx/xxx/xxx/node_modules/bluebird/js/release/promise.js:604:18)
at Promise._settlePromise0 (/Users/xxx/xxx/xxx/node_modules/bluebird/js/release/promise.js:649:10)
at Promise._settlePromises (/Users/xxx/xxx/xxx/node_modules/bluebird/js/release/promise.js:725:18)
at _drainQueueStep (/Users/xxx/xxx/xxx/node_modules/bluebird/js/release/async.js:93:12)
at _drainQueue (/Users/xxxx/xxx/xxx/node_modules/bluebird/js/release/async.js:86:9)
at Async._drainQueues (/Users/xxx/xxx/xxx/node_modules/bluebird/js/release/async.js:102:5)
at Immediate.Async.drainQueues [as _onImmediate] (/Users/xxx/xxx/xxx/node_modules/bluebird/js/release/async.js:15:14)
at processImmediate (internal/timers.js:461:21) {
parent: error: there is no unique or exclusion constraint matching the ON CONFLICT specification
at Parser.parseErrorMessage (/Users/xxx/xxx/xxx/node_modules/pg-protocol/dist/parser.js:278:15)
at Parser.handlePacket (/Users/xxx/xxx/xxx/node_modules/pg-protocol/dist/parser.js:126:29)
at Parser.parse (/Users/xxxx/xxx/xxx/node_modules/pg-protocol/dist/parser.js:39:38)
at Socket.<anonymous> (/Users/xxx/xxx/xxx/node_modules/pg-protocol/dist/index.js:10:42)
at Socket.emit (events.js:315:20)
at Socket.EventEmitter.emit (domain.js:467:12)
at addChunk (internal/streams/readable.js:309:12)
at readableAddChunk (internal/streams/readable.js:284:9)
at Socket.Readable.push (internal/streams/readable.js:223:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
length: 148,
severity: 'ERROR',
code: '42P10',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'plancat.c',
line: '826',
routine: 'infer_arbiter_indexes',
sql: `INSERT INTO "public"."Capability" ("id","name","description","categoryId","organizationId","deleted","updatedAt","updatedById","createdAt","createdById","underAssessment","capabilityStatusId","riskScore","accmtName","acmmtDescription","completeFlag") VALUES (1157,'Capability 2(updated)','test1',1,183,false,'2021-03-02 17:51:29.384 +00:00',204,'2021-03-02 17:51:29.384 +00:00',204,false,1,0,'(xxx)','(xxx)',false),(1156,'Capability 1(updated)','test2',1,183,false,'2021-03-02 17:51:29.384 +00:00',204,'2021-03-02 17:51:29.384 +00:00',204,false,1,0,'(xxx)','(xxx)',false) ON CONFLICT ("categoryId","updatedById","createdById","capabilityStatusId") DO UPDATE SET "name"=EXCLUDED."name","description"=EXCLUDED."description","categoryId"=EXCLUDED."categoryId","organizationId"=EXCLUDED."organizationId","updatedById"=EXCLUDED."updatedById","createdById"=EXCLUDED."createdById","accmtName"=EXCLUDED."acmmtDescription","acmmtDescription"=EXCLUDED."acmmtDescription" RETURNING *;`,
parameters: undefined
},
Additional context
Add any other context or screenshots about the feature request here.
Environment
- Sequelize version: 5.21.3
- Node.js version: 14.15.3
- Operating System: MacOS Catalina, version 10.15.7
- 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):
- 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.
Activity
feat(model): add support for where clauses with ON CONFLICT queries
github-actions commentedon Oct 27, 2021
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂