sequelize / sequelize/sequelize

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

Open
#11,090 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

docs existing workaround status: wip
Dominant language
TypeScript
Stars
30.4k
Forks
4.3k
Avg merge
1d 6h
Merged PRs (30d)
68

Description

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:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example through the Asset.bulkCreate entry point against MySQL 5.7, then inspect how updateOnDuplicate is handled for the MySQL dialect. Add regression coverage for the default behavior and verify that the duplicate primary-key row is updated without explicitly supplying the option.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mysql
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.