'uniqueViolated' error type when inserting the same record more than once
See original GitHub issueWhen 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:
- Created 9 years ago
- Comments:9 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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/
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.