Repository navigation
Description
Issue Description
bulkCreate with updateOnDuplicate throws error when one of the model's primary key fields has a different name from its DB column name.
bulkCreate generates the field names in the: ON CONFLICT (column1...) part of the query by looking at: this.primaryKeys.fieldName which is the model's field name, instead of looking at this.primaryKeys.field which is the DB's column name.
The error received by Postgres and thrown by Sequelize is, expectedly: Unhandled rejection SequelizeDatabaseError: column "fieldName" does not exist
What are you doing?
Minimal self contained reproducible example:
const Sequelize = require('sequelize');
const sequelize = new Sequelize('postgres://postgres:postgres@localhost:5432/postgres');
class User extends Sequelize.Model {
}
User.init({
userName: {
type: Sequelize.STRING,
primaryKey: true,
field: 'user_name',
},
email: {
type: Sequelize.STRING,
}
},
{ sequelize }
);
const user1 = {
userName: 'John Smith',
};
User.sync({ force: true }).then(() =>
User.bulkCreate([user1], {updateOnDuplicate: ['email']})
// throws: Unhandled rejection SequelizeDatabaseError: column "userName" does not exist
);What do you expect to happen?
The code above should run without errors.
bulkCreate with updateOnDuplicate should work even when primary key fields are named differently in the DB.
What is actually happening?
Sequelize throws: Unhandled rejection SequelizeDatabaseError: column "userName" does not exist
Additional context
The code above works if the field name is changed to user_name.
The bug is in this line:
Line 2703 in d5e4a10
| options.upsertKeys = _.chain(model.primaryKeys).values().map('fieldName').value(); |
fieldName should be field.
A PR to fix this: #11434
Environment
- Sequelize version: 5.8.14
- Node.js version: 8.15.0
- Operating System: macOS Mojave 10.14.6
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
- 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
papb commentedon Sep 15, 2019
Hello! Thank you very much!!
papb commentedon Sep 15, 2019
I noticed this problem also exists for SQLite.
PeledYuval commentedon Sep 15, 2019
Thanks, I didn't notice that!
You're right, AbstractQueryGenerator contains the following code that looks at
options.upsertKeyswhich this PR fixes:sushantdhiman commentedon Sep 19, 2019
#11434
jpvanoosten commentedon Sep 19, 2019
The
bulkCreatealso fails if the model contains a composite key and doesn't explicitly define a primary key.The
upsertKeysarray is emptied when the model contains a composite key since the length of the fields array of thecompositeKeyobject will be of size > 1 (which causes this filter to skip it):sequelize/lib/model.js
Line 2705 in 3a60069
My model looks like this:
In this case, the
model.uniqueKeysobject only contains a singlecompositeKeyobject.Shouldn't the
model.uniqueKeysobject also contain the automatically generatedidprimary key for this model?Changing the model to this works:
papb commentedon Sep 25, 2019
@jpvanoosten Thanks for the detailed report, can you please open a new issue, and link to this one? Thanks