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.

'uniqueViolated' error type when inserting the same record more than once

See original GitHub issue

When inserting the same record in a database more than once I expect different unique keys to be associated to them, and the resulting database to have one record for every time I’ve inserted the record.

This is not happening though. The sample code below:

var async = require('async'),
    Nedb = require('nedb'),
    path = require('path'),
    db = new Nedb({ filename: path.join(__dirname, 'foobar.db'), autoload: true });

var record = { 'foo': 'bar' };

async.eachSeries([0, 1, 2, 3, 4], function (recordNo, callback) {
    console.log("Inserting record no. " + recordNo);
    db.insert(record, function (err) {
        if (err) console.log(err);
        callback(err);
    });
}, function (err) {
    console.log("Done");
});

has as output:

$ node test3.js --test 
Inserting record no. 0
Inserting record no. 1
{ message: 'Can\'t insert key mHJCoBF42NffD0jy, it violates the unique constraint',
  key: 'mHJCoBF42NffD0jy',
  errorType: 'uniqueViolated' }
Done
$ 

foobar.db is:

{"foo":"bar","_id":"mHJCoBF42NffD0jy"}

Am I missing something? I can’t be the first person to do something like this. Thanks.

G.

Issue Analytics

  • State:open
  • Created 9 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
obinetbitscommented, Jun 4, 2016

You must have objects in your database, and not simple type such as string or number. Otherwise, you will end up with this error:

{ message: ‘Can't insert key undefined, it violates the unique constraint’, key: undefined, errorType: ‘uniqueViolated’ }

because every record needs a _id properly (generated by the NeDB), so if you don’t have an object, it cannot add the _id property.

for more details see “Where are my data ?” section on http://ctheu.com/2015/02/09/a-journey-through-the-creation-of-an-app-with-node-mongodb-react-gulp/

1reaction
mcallan83commented, Mar 29, 2017

I had this issue as well. I was about to post here asking for updates, but realized I had set a unique index on a field, but the objects I was upserting did not contain that property.

This caused the first record to be inserted, because the unique field had a value of undefined, but the next records would not insert because they had the same values for the unique field.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Handle the Psycopg2 UniqueViolation Error in Python
The psycopg2.errors.UniqueViolation is an error thrown by the when a user attempts to insert a duplicate key value.
Read more >
UniqueViolation: ERROR: duplicate key value violates unique ...
With a unique index you will only be able to have one record with the same reportable_type and reportable_id .
Read more >
PG::UniqueViolation: ERROR: duplicate key value violates ...
I recall that for ci_pipelines there's a small transaction ahead of the actual work that generates the internal id (and the record in ......
Read more >
Hidden dangers of duplicate key violations in PostgreSQL and ...
The “duplicate key violates unique constraint” error notifies the caller ... The INSERT 0 1 depicts that one row was inserted successfully.
Read more >
Error while inserting rows in a table - Oracle Communities
Also, one piece of info which i would like to share here is that on this table, we have a unique index which...
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