Repository navigation
Description
What are you doing?
Hi guys,
I have found that bulkCreate with updateOnDuplicate option is not mapping fields correctly when I use snake-case in the mysql structure and camel-case in model definitions.
Model:
const Model = sequelize.define('userCallUp', {
id: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
primaryKey: true,
autoIncrement: true,
field: 'id'
},
idCallUp: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: false,
references: {
model: 'callUp',
key: 'id'
},
field: 'id_call_up'
},
idUser: {
type: DataTypes.INTEGER(10).UNSIGNED,
allowNull: true,
references: {
model: 'user',
key: 'id'
},
field: 'id_user'
},
number: {
type: DataTypes.INTEGER(3).UNSIGNED,
allowNull: true,
field: 'number'
},
nickname: {
type: DataTypes.CHAR(255),
allowNull: true,
field: 'nickname'
},
group: {
type: DataTypes.ENUM('bench','pitch','coachs'),
allowNull: false,
defaultValue: 'bench',
field: 'group'
},
createdAt: {
type: DataTypes.DATE,
allowNull: true,
field: 'created_at'
},
updatedAt: {
type: DataTypes.DATE,
allowNull: true,
field: 'updated_at'
},
deletedAt: {
type: DataTypes.DATE,
allowNull: true,
field: 'deleted_at'
}
}, {
tableName: 'user_call_up'
});When calling:
const usersCallsUp = [{
"idCallUp": 21,
"idUser": 500,
"number": null,
"nickname": null,
"group": "coachs"
},{
"idCallUp": 21,
"idUser": 501,
"number": 1,
"nickname": null,
"group": "pitch"
}];
UserCallUp.bulkCreate(usersCallsUp, {updateOnDuplicate: ['idUser', 'number', 'nickname', 'group']});
What do you expect to happen?
I expect that all fields that are including in the updateOnDuplicate option were mapping propertly on ON DUPLICATE KEY UPDATE clause of SQL query.
Expected SQL query:
INSERT INTO `user_call_up` (`id`,`id_call_up`,`id_user`,`group`,`created_at`,`updated_at`,`number`) VALUES (NULL,21,500,'coachs','2018-02-08 17:14:09','2018-02-08 17:14:09',NULL),(NULL,21,501,'pitch','2018-02-08 17:14:09','2018-02-08 17:14:09',1) ON DUPLICATE KEY UPDATE `id_user`=VALUES(`id_user`),`number`=VALUES(`number`),`nickname`=VALUES(`nickname`),`group`=VALUES(`group`);
What is actually happening?
A sequelize error is raised because on ON DUPLICATE KEY UPDATE the field "idUser" is not mapping correctly to "id_user" which is the correct field on database. (camelCase to snake_case)
Sequelize error:
{ SequelizeDatabaseError: Unknown column 'idUser' in 'field list' ... }
SQL query:
INSERT INTO `user_call_up`
(`id`,`id_call_up`,`id_user`,`group`,`created_at`,`updated_at`,`number`) VALUES (NULL,21,500,'coachs','2018-02-08 17:14:09','2018-02-08 17:14:09',NULL),(NULL,21,501,'pitch','2018-02-08 17:14:09','2018-02-08 17:14:09',1) ON DUPLICATE KEY UPDATE `idUser`=VALUES(`idUser`),`number`=VALUES(`number`),`nickname`=VALUES(`nickname`),`group`=VALUES(`group`);
Also, if you include the value of database field "id_user" into the array like that, ['id_user', 'number', 'nickname', 'group'], sequelize ignore this field to the clause.
Dialect: mysql
Dialect version: 2.11.1
Database version: 5.7.18
Sequelize version: 4.33.0
Tested with latest release: 4.33.0
Thanks!
Manu Rua
Activity
fix(bulkCreate): fix field name mapping issue for bulk create queries…
sushantdhiman commentedon Mar 13, 2018
🎉 This issue has been resolved in version 4.36.1 🎉
The release is available on:
Your semantic-release bot 📦🚀
arinhouck commentedon Nov 8, 2019
I am experiencing same issue on Postgres
From defined model
Query contains
Code executed
Dialect: postgres
Database version: 10.7
Sequelize version: 5.2.21
sushantdhiman commentedon Nov 22, 2019
Fixed by #11666