question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`bulkCreate`'s `updateOnDuplicate` option default does not work

See original GitHub issue

What are you doing?

I am creating 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.

await Asset.destroy({ where: {} });

await Asset.create({
    id: 1,
    uuid: 'uuid-1',
    name: 'asset-1',
});

await Asset.bulkCreate([
    {
        id: 2,
        uuid: 'uuid-2',
        name: 'asset-2',
    },
    {
        id: 1,
        uuid: 'uuid-updated',
        name: 'asset-updated',
    },
],{
    // updateOnDuplicate: ['name'],
});

To Reproduce Steps to reproduce the behavior:

  1. Define model Asset:
Asset.init(
	{
		id: {
			type: Sequelize.INTEGER,
			primaryKey: true,
		},
		uuid: {
			type: Sequelize.UUID,
		},
		name: {
			type: Sequelize.STRING,
			allowNull: false,
		},
	},
	{
		sequelize,
	}
);
  1. Run the code above
  2. See a SequelizeUniqueConstraintError

What do you expect to happen?

I wanted

const assets = await Asset.findAll({ where: {} });
console.log(assets.map(a => a.toJSON()));

to produce:

[ { id: 1,
    uuid: 'uuid-updated',
    name: 'asset-updated',
    createdAt: 2019-06-20T13:04:53.000Z,
    updatedAt: 2019-06-20T13:04:53.000Z },
  { id: 2,
    uuid: 'uuid-2',
    name: 'asset-2',
    createdAt: 2019-06-20T13:04:53.000Z,
    updatedAt: 2019-06-20T13:04:53.000Z } ]

What is actually happening?

The mentioned code produces

SequelizeUniqueConstraintError: Validation error
      at Query.formatError (node_modules/sequelize/lib/dialects/mysql/query.js:218:16)
      at Query.handler [as onResult] (node_modules/sequelize/lib/dialects/mysql/query.js:46:23)
      at Query.execute (node_modules/mysql2/lib/commands/command.js:30:14)
      at Connection.handlePacket (node_modules/mysql2/lib/connection.js:449:32)
      at PacketParser.Connection.packetParser.p [as onPacket] (node_modules/mysql2/lib/connection.js:72:12)
      at PacketParser.executeStart (node_modules/mysql2/lib/packet_parser.js:75:16)
      at Socket.Connection.stream.on.data (node_modules/mysql2/lib/connection.js:79:25)
      at addChunk (_stream_readable.js:263:12)
      at readableAddChunk (_stream_readable.js:250:11)
      at Socket.Readable.push (_stream_readable.js:208:10)
      at TCP.onread (net.js:594:20)

The output is as expected if I pass updateOnDuplicate: ['name', 'uuid'] as option to Asset.bulkCreate, but I want the option to default to that by itself.

Environment

Dialect:

  • mysql
  • postgres
  • sqlite
  • mssql
  • any Dialect library version: “mysql2”: “1.6.5” Database version: 5.7.23 Sequelize version: “5.8.7” Node Version: “8.9.3” OS: Linux Tested with latest release:
  • No
  • Yes, specify that version:

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
ustuncommented, Jan 29, 2020

We use Object.keys(Model.rawAttributes)

3reactions
ozhicommented, Jul 25, 2019

Yes, it does work when I manually provide the property names. However, I want to use the default in order not to have to change it when I add/remove properties to my model.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mysql - updateOnDuplicate not effect - Stack Overflow
As you can see, I make the typeId unique and execute IotNode.bulkCreate twice. The generated SQL logs: Executing (default): INSERT INTO ...
Read more >
BulkCreateOptions | @sequelize/core
If false the applicable hooks will not be called. The default value depends on the context. ... BulkCreate hooks will still be run...
Read more >
Hooks - Manual | Sequelize
bulkCreate (...) with the updateOnDuplicate option, changes made in the hook to fields that aren't given in the updateOnDuplicate array will not be...
Read more >
13.2.7.2 INSERT ... ON DUPLICATE KEY UPDATE Statement
If you specify an ON DUPLICATE KEY UPDATE clause and a row to be inserted would cause a duplicate value in a UNIQUE...
Read more >
BulkCreateOptions | Guido
(not supported by postgres). Defaults to false ... BulkCreate hooks will still be run if options.hooks is true. ... By default, all fields...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found