Skip to content

BulkCreate: updateOnDuplicate option is not mapping fields correctly #9016

Closed

Description

@ManuRua

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

  1. added
    type: bug
    on Feb 9, 2018
  2. sushantdhiman commented on Mar 13, 2018

    @sushantdhiman
    Contributor

    🎉 This issue has been resolved in version 4.36.1 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

  3. arinhouck commented on Nov 8, 2019

    @arinhouck

    I am experiencing same issue on Postgres

    From defined model

    externalId: {
            type: DataTypes.TEXT,
            allowNull: true,
            comment: "null",
            unique: true,
            field: "external_id"
          },
    

    Query contains

    CONFLICT ("externalId") DO UPDATE SET "name"=EXCLUDED."name" RETURNING *;
    

    Code executed

    db.Location.bulkCreate(payload, {
          updateOnDuplicate: ["name"]
        });
    

    Dialect: postgres
    Database version: 10.7
    Sequelize version: 5.2.21

  4. sushantdhiman commented on Nov 22, 2019

    @sushantdhiman
    Contributor

    Fixed by #11666

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions