Skip to content

ignoreDuplicates not inherited to included models on bulkCreate #11605

@benjiewheeler

Description

@benjiewheeler

We have a two models: User and Article, where User.hasMany(Article).

if we bulkCreate some data with ignoreDuplicates set to true, it's not propagated to the included models, and throws an SequelizeUniqueConstraintError in case of duplicate data.

As you can see in the logs below the call to insert data to user is done with INSERT OR IGNORE INTO but the rest of the commands to insert to article is done using INSERT INTO

Shouldn't the rest of the calls be done using INSERT OR IGNORE ?

Code used to replicate this:

const Sequelize = require('sequelize');
const sqliteSeq = new Sequelize('sqlite::memory:');

const User = sqliteSeq.define("user", {
  name: Sequelize.STRING
});

const Article = sqliteSeq.define("article", {
  uid: {
    type: Sequelize.INTEGER,
    unique: true
  },
  name: Sequelize.STRING,
});

User.hasMany(Article);

let data = [
  {
    name: "John",
    articles: [{ uid: 1, name: "Sample Article Name" }]
  }, {
    name: "Mike",
    articles: [{ uid: 1, name: "Another Article Name", }] // Article with already existing uid
  }
];

sqliteSeq.sync().then(() => User.bulkCreate(data, {
  ignoreDuplicates: true,
  include: [Article]
}))
.then(data => console.log(data.map(r => r.toJSON())))
.catch(console.error);

Here is the full console log:

Executing (default): CREATE TABLE IF NOT EXISTS `users` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL);
Executing (default): PRAGMA INDEX_LIST(`users`)
Executing (default): CREATE TABLE IF NOT EXISTS `articles` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `uid` INTEGER UNIQUE, `name` VARCHAR(255), `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `userId` INTEGER REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE);
Executing (default): PRAGMA INDEX_LIST(`articles`)
Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_articles_1`)
Executing (default): INSERT OR IGNORE INTO `users` (`id`,`name`,`createdAt`,`updatedAt`) VALUES (NULL,'John','2019-10-24 16:41:55.629 +00:00','2019-10-24 16:41:55.629 +00:00'),(NULL,'Mike','2019-10-24 16:41:55.629 +00:00','2019-10-24 16:41:55.629 +00:00');
Executing (default): INSERT INTO `articles` (`id`,`uid`,`name`,`createdAt`,`updatedAt`,`userId`) VALUES (NULL,1,'Sample Article Name','2019-10-24 16:41:55.629 +00:00','2019-10-24 16:41:55.629 +00:00',1),(NULL,1,'Another Article Name','2019-10-24 16:41:55.629 +00:00','2019-10-24 16:41:55.629 +00:00',2);
UniqueConstraintError [SequelizeUniqueConstraintError]: Validation error
    at Query.formatError (/app/sessions/3iwy0644icg00/node_modules/sequelize/lib/dialects/sqlite/query.js:416:16)
    at Query._handleQueryResponse (/app/sessions/3iwy0644icg00/node_modules/sequelize/lib/dialects/sqlite/query.js:73:18)
    at Statement.afterExecute (/app/sessions/3iwy0644icg00/node_modules/sequelize/lib/dialects/sqlite/query.js:250:31)
    at Statement.replacement (/app/sessions/3iwy0644icg00/node_modules/sqlite3/lib/trace.js:19:31) {
  name: 'SequelizeUniqueConstraintError',
  errors: [
    ValidationErrorItem {
      message: 'uid must be unique',
      type: 'unique violation',
      path: 'uid',
      value: null,
      origin: 'DB',
      instance: null,
      validatorKey: 'not_unique',
      validatorName: null,
      validatorArgs: []
    }
  ],
  fields: [ 'uid' ],
  parent: [Error: SQLITE_CONSTRAINT: UNIQUE constraint failed: articles.uid] {
    errno: 19,
    code: 'SQLITE_CONSTRAINT',
    sql: "INSERT INTO `articles` (`id`,`uid`,`name`,`createdAt`,`updatedAt`,`userId`) VALUES (NULL,1,'Sample Article Name','2019-10-24 16:41:55.629 +00:00','2019-10-24 16:41:55.629 +00:00',1),(NULL,1,'Another Article Name','2019-10-24 16:41:55.629 +00:00','2019-10-24 16:41:55.629 +00:00',2);"
  },
  original: [Error: SQLITE_CONSTRAINT: UNIQUE constraint failed: articles.uid] {
    errno: 19,
    code: 'SQLITE_CONSTRAINT',
    sql: "INSERT INTO `articles` (`id`,`uid`,`name`,`createdAt`,`updatedAt`,`userId`) VALUES (NULL,1,'Sample Article Name','2019-10-24 16:41:55.629 +00:00','2019-10-24 16:41:55.629 +00:00',1),(NULL,1,'Another Article Name','2019-10-24 16:41:55.629 +00:00','2019-10-24 16:41:55.629 +00:00',2);"
  },
  sql: "INSERT INTO `articles` (`id`,`uid`,`name`,`createdAt`,`updatedAt`,`userId`) VALUES (NULL,1,'Sample Article Name','2019-10-24 16:41:55.629 +00:00','2019-10-24 16:41:55.629 +00:00',1),(NULL,1,'Another Article Name','2019-10-24 16:41:55.629 +00:00','2019-10-24 16:41:55.629 +00:00',2);"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugDEPRECATED: replace with the "bug" issue type

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions