Skip to content
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 do not work with individualHooks #10461

Open
Crusader4Christ opened this issue Feb 19, 2019 · 11 comments
Open
Labels
status: understood For issues. Applied when the issue is understood / reproducible. topic: hooks For issues and PRs. Things involving hooks. type: bug

Comments

@Crusader4Christ
Copy link

Crusader4Christ commented Feb 19, 2019

What are you doing?

Model.bulkCreate(data, {updateOnDuplicate:["value1","value2"], individualHooks:true})

What do you expect to happen?

INSERT INTO `model` (`id`,`value1`,`value2`) 
VALUES (1,'v1','v2') 
ON DUPLICATE KEY UPDATE `value1`=VALUES(`value1`), `value2`=VALUES(`value2`); 

What is actually happening?

INSERT INTO `model` (`id`,`value1`,`value2`) VALUES (1,'v1','v2')

Dialect: mysql
Dialect version: XXX
__Database version: 5.7
__Sequelize version: 4.42.0
__Tested with latest release:4.42.0

Bug is https://github.com/sequelize/sequelize/blob/v4/lib/model.js#L2406 modelInstance.save do not handle updateOnDuplicate.
It can be fixed if https://github.com/sequelize/sequelize/blob/v4/lib/query-interface.js#L896
will check updateOnDuplicate for mySQL and use upsertQuery in this case

@AndyBaird
Copy link

AndyBaird commented Mar 5, 2019

I am having a similar problem with getting the updateOnDuplicate option to work.

What are you doing?

[err, response] = await to(Lists.bulkCreate(req.body,{ updateOnDuplicate:['id','order'], }));

What do you expect to happen?

ON DUPLICATE KEY UPDATE should get appended but it does not, so the queries fail

What is actually happening?

Executing (default): INSERT INTO `lists` (`id`,`order`,`original`,`completed`,`archive`,`subheading`,`public`,`style`,`createdAt`,`updatedAt`,`hierarchyLevel`) VALUES (15,12,true,false,false,false,true,0,'2019-03-05 00:17:48','2019-03-05 00:17:48',1);`

@stale

This comment has been minimized.

@stale stale bot added the stale label Jun 3, 2019
@JovanovicJovan

This comment has been minimized.

@stale stale bot removed the stale label Jun 7, 2019
@papb
Copy link
Member

papb commented Jul 25, 2019

Tested with latest release:4.42.0

Can someone please test with the latest 5.x version?

@papb papb added status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: bug labels Jul 25, 2019
@sushantdhiman
Copy link
Contributor

This fails because save / create does not support updateOnDuplicate which is used when using individualHooks

const Sequelize = require('./index');
const sequelize = require('./test/support').createSequelizeInstance();
const Op = Sequelize.Op;

const Model = sequelize.define('Model', {
  name: Sequelize.STRING,
  company: {
    type: Sequelize.STRING,
    unique: true
  }
});

(async () => {
  await Model.sync({ force: true });

  await Model.bulkCreate([
    {
      name: 'abc',
      company: 'c1'
    },
    {
      name: 'xyz',
      company: 'c2'
    }
  ]);

  const result = await Model.bulkCreate([
    {
      name: 'cds',
      company: 'c1'
    },
    {
      name: 'xsyz',
      company: 'c2'
    }
  ], {
    updateOnDuplicate: ['name'],
    individualHooks: true
  });

  console.log(result);
})();

@sushantdhiman sushantdhiman added status: understood For issues. Applied when the issue is understood / reproducible. and removed status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action labels Jul 27, 2019
@Somebi

This comment has been minimized.

@anz-davar
Copy link

same issue with sequelize 5.15.1

@papb papb added the topic: hooks For issues and PRs. Things involving hooks. label Jan 16, 2020
@djustclean
Copy link

guys i am using bulkCreate at too many places. it works fine but thing is if query finds and unique key failure then it updates otherwise it creates. you should keep in mind about unique key to pass in you query like

const result = await Model.bulkCreate([
{
id: 1,
name: 'cds',
company: 'c1'
},
{
id: null,
name: 'xsyz',
company: 'c2'
}
], {
updateOnDuplicate: ['name'],
individualHooks: true
});

@djustclean
Copy link

so here id is primary key or unique if it exists then it will update otherwise it will create

@pushkar0108
Copy link

Do we have any update on this or some work around?

@aneeq-ur-rehman4
Copy link

aneeq-ur-rehman4 commented Sep 20, 2023

This could have been the most awesome feature. But this bug is ruining it. A little attention to this issue will be appreciated. Regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: understood For issues. Applied when the issue is understood / reproducible. topic: hooks For issues and PRs. Things involving hooks. type: bug
Projects
None yet
Development

No branches or pull requests

10 participants