sequelize: bulkCreate with associations and ignoreDuplicates: true creates duplicate records in child tables

Hello all,

I’m using bulkCreate along with ignoreDuplicates: true as per the code below:

const convDB = await Item.bulkCreate(items, { ignoreDuplicates: true, include: [EmailAddress], });

When I run this code with the same payload more than one time, the records are created in the parent table “item” without duplicates, but in the child table “email_address”, several duplicates are created. From my understanding no duplicates should be created in the child tables. I’m not completely sure if I’m doing something wrong or if it is effectively and issue.

Relevant info: Sequelize ver: “^6.25.8” DB: PostgreSQL 11.17

Please see my models and associations:

sequelize.define('item', { item_id: { type: DataTypes.TEXT, primaryKey: true, unique: true, }, conv_id: { type: DataTypes.TEXT, allowNull: false, }, subject: { type: DataTypes.TEXT, }, html_body: { type: DataTypes.TEXT, }, headers_content: { type: DataTypes.TEXT, }, received_time: { type: DataTypes.DATE, allowNull: false, }, });

sequelize.define('email_address', { email_address_id: { autoIncrement: true, type: DataTypes.BIGINT, primaryKey: true, }, address: { type: DataTypes.STRING(320), allowNull: false, }, });

Item.hasMany(EmailAddress, { foreignKey: { name: 'item_id', allowNull: false }, });

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 1
  • Comments: 15 (4 by maintainers)

Most upvoted comments

@WikiRik @markchure @ephys ,

I’ve figured how to add ignoreDuplicate flag on child table if parent table has the same flag. we’ll need to add below line in models.js file in recursiveBulkCreate function.

includeOptions.ignoreDuplicates=options2.ignoreDuplicates

I have tested this in my local machine and and it’s working as expected. can i create a PR for this ?

hi @markchure ,

I haven’t had the chance to work on the issue. will try this week.

i’m looking into how “ignoreDuplicates” can be applied to child tables as well.