New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bulkCreate with updateOnDuplicate not returning primary key for new record #11223
Comments
Can you check with another dialect, such as SQLite for instance? |
Hi papb, Model: {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
unique: true
set() {
let value = this.getDataValue('id')
this.setDataValue('id', value);
},
},
name: Sequelize.TEXT,
value: Sequelize.TEXT
} Thank you for your response. |
But if i set the value in the model it is duplicating all the records and not updating |
I see... You posted only a code snippet, can you provide a SSCCE (also known as MCVE/reprex) to easily reproduce it all by copy-pasting? |
MySQL only returns ids for records which got inserted with const Sequelize = require('./index');
const sequelize = require('./test/support').createSequelizeInstance();
const Op = Sequelize.Op;
const Model = sequelize.define('Model', {
name: Sequelize.STRING,
value: Sequelize.STRING
});
(async () => {
await Model.sync({ force: true });
await Model.bulkCreate([
{id: 1, name: 'name1', value: 'value1'},
{id: 2, name: 'name2', value: 'value2'}
]);
const result = await Model.bulkCreate([
{id: 1, name: 'name1', value: 'value1'},
{id: 2, name: 'name2', value: 'value2'},
{ name: 'name3', value: 'value3'}
], {
updateOnDuplicate: ['name', 'value']
});
console.log(JSON.stringify(result, null, 2));
})(); Executing (default): DROP TABLE IF EXISTS `Models`;
Executing (default): CREATE TABLE IF NOT EXISTS `Models` (`id` INTEGER NOT NULL auto_increment , `name` VARCHAR(255), `value` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): SHOW INDEX FROM `Models`
Executing (default): INSERT INTO `Models` (`id`,`name`,`value`,`createdAt`,`updatedAt`) VALUES (1,'name1','value1','2019-07-27 09:02:18','2019-07-27 09:02:18'),(2,'name2','value2','2019-07-27 09:02:18','2019-07-27 09:02:18');
[ { id: 2 }, { id: 3 } ]
Executing (default): INSERT INTO `Models` (`id`,`name`,`value`,`createdAt`,`updatedAt`) VALUES (1,'name1','value1','2019-07-27 09:02:18','2019-07-27 09:02:18'),(2,'name2','value2','2019-07-27 09:02:18','2019-07-27 09:02:18'),(NULL,'name3','value3','2019-07-27 09:02:18','2019-07-27 09:02:18') ON DUPLICATE KEY UPDATE `name`=VALUES(`name`),`value`=VALUES(`value`);
[ { id: 3 } ]
[
{
"id": 1,
"name": "name1",
"value": "value1",
"createdAt": "2019-07-27T09:02:18.974Z",
"updatedAt": "2019-07-27T09:02:18.974Z"
},
{
"id": 2,
"name": "name2",
"value": "value2",
"createdAt": "2019-07-27T09:02:18.974Z",
"updatedAt": "2019-07-27T09:02:18.974Z"
},
{
"id": null,
"name": "name3",
"value": "value3",
"createdAt": "2019-07-27T09:02:18.974Z",
"updatedAt": "2019-07-27T09:02:18.974Z"
}
]
|
Possibly the same problem on
output:
so note how However, if I inspect the table on the shell after running the script:
we see that
SQLite just added More context: https://stackoverflow.com/questions/54866952/how-to-ignore-sequelizeuniqueconstrainterror-in-sequelize/68040264#68040264 |
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. 🙂 |
Setting
|
What are you doing?
To Reproduce
Steps to reproduce the behavior:
model:
What do you expect to happen?
To return the primary key for all objects
What is actually happening?
I have inserted the first 2 data earlier and while updating have added the 3rd object to the array.
while getting response i am getting primary key's for first 2 data and null for newly added object.
response :
Environment
Dialect: - mysql
Dialect library version: ^2.16.0
Sequelize version: ^5.2.0
Node Version: 10.15.3
OS: linux
The text was updated successfully, but these errors were encountered: