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.

Create/Save ignoreDuplicates Option

See original GitHub issue

bulkCreate got an ignoreDuplicates option based on this issue.

I can see there is no ignoreDuplicates option for create or save but is there a way to INSERT IGNORE when inserting one row?

Use Case:

I am in the process of refactoring Community Casts to use sequelize.

I want to port this line of code:

return connection.queryAsync('INSERT IGNORE INTO tags VALUES ?', values);

Here is a an example:

var Tag = sequelize.define('Tag', {
  name: {
    type: Sequelize.STRING,
    primaryKey: true
  }
});

sequelize.sync({ force: true }).then(function () {
  Tag.create({
    name: 'Hello'
  }).then(function () {
    // boom!
    Tag.create({
      name: 'Hello'
    });
  });
});

And the exception:

Executing (default): INSERT INTO `Tags` (`name`,`updatedAt`,`createdAt`) VALUES ('Hello','2015-09-15 08:51:44','2015-09-15 08:51:44');
Unhandled rejection SequelizeUniqueConstraintError: Validation error
    at Query.formatError (c:\Users\alexb\Documents\Code\demo\node_modules\sequelize\lib\dialects\mysql\query.js:134:14)
    at Query._callback (c:\Users\alexb\Documents\Code\demo\node_modules\sequelize\lib\dialects\mysql\query.js:35:21)
    at Query.Sequence.end (c:\Users\alexb\Documents\Code\demo\node_modules\mysql\lib\protocol\sequences\Sequence.js:96:24)
    at Query.ErrorPacket (c:\Users\alexb\Documents\Code\demo\node_modules\mysql\lib\protocol\sequences\Query.js:94:8)
    at Protocol._parsePacket (c:\Users\alexb\Documents\Code\demo\node_modules\mysql\lib\protocol\Protocol.js:274:23)
    at Parser.write (c:\Users\alexb\Documents\Code\demo\node_modules\mysql\lib\protocol\Parser.js:77:12)
    at Protocol.write (c:\Users\alexb\Documents\Code\demo\node_modules\mysql\lib\protocol\Protocol.js:39:16)
    at Socket.<anonymous> (c:\Users\alexb\Documents\Code\demo\node_modules\mysql\lib\Connection.js:96:28)
    at Socket.emit (events.js:107:17)
    at readableAddChunk (_stream_readable.js:163:16)
    at Socket.Readable.push (_stream_readable.js:126:10)
    at TCP.onread (net.js:538:20)

Ideally I would be able to use the nested syntax:

Screencast.create({
  id: '123',
  title: 'Sequelize video.',
  Tags: [{ name:'Sequelize' }, { name:'Node' }]
}, {
  include: [Tag]
}

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:3
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
mickhansencommented, Sep 15, 2015

@alexbooker should be possible, you could then just ignore a unique constraint error with .catch(Sequelize.UniqueConstraintError, noop)

1reaction
adams-familycommented, Nov 29, 2021

+1

I would be very happy to see the ignoreDuplicates option for a single create if anyone finds a solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - BulkCreate ignore duplicate values for custom fields
I'm looking for some way in the bulkCreate() method to specify 'ignoreDuplicates' with my custom key 'title' and then use 'updateOnDuplicate' to ...
Read more >
Pinnacle Studio 25 User Guide
item or right-clicking and choosing an option from the context menu. ... Title page includes basic create, save, load and duration controls.
Read more >
BulkCreateOptions | @sequelize/core
Options for Model. ... Pass query execution time in milliseconds as second argument to logging function (options.logging). ... Optional ignoreDuplicates.
Read more >
SAP BusinessObjects Live Data Connect Installation and ...
This document provides SAP BusinessObjects Business Intelligence administrators with information, procedures and options for the installation of ...
Read more >
Content Composer Studio - Hyland Software Products
In the Filter tab, select one of the following options. ... Select the Use SQL Server Authentication option. ... 0 = Ignore -...
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