Unique index validation error not throwing
See original GitHub issueI’m using this schema with latest npm mongoose:
var schema = new Schema({
_id: Schema.ObjectId,
email: {type: String, required: true, unique: true}
});
If I try to save a email that is already in db, I expect to get a ValidationError
like if a required
field is omitted. However this is not the case, I get a MongoError: E11000 duplicate key error index
.
Which is not a validation error (happens even if I remove the unique:true).
Issue Analytics
- State:
- Created 11 years ago
- Comments:13
Top Results From Across the Web
node.js - Mongoose unique validation error even though ...
You may need to confirm this first by checking the indexes currently in the database; you will most probably find the email unique...
Read more >How to handle unique constraint violations
Catch uniqueness exceptions thrown by the database at the lowest level possible — in the UnitOfWork class · Convert them into Result ·...
Read more >Customise Index errors | OutSystems
Is it possible to customise the server error message that shows up from a unique index when a user tries to enter a...
Read more >Unique Indexes — MongoDB Manual
A unique index ensures that the indexed fields do not store duplicate values; i.e. enforces uniqueness for the indexed fields. By default, MongoDB...
Read more >Difference between Unique Indexes and Unique Constraints ...
We cannot drop the unique Index created by the unique constraints. If we try to do so, it gives you the following error...
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
For anyone interested, I’ve added a plugin which checks the DB prior to saving and throws a Mongoose validation error if any of your unique constraints will be violated: https://github.com/blakehaswell/mongoose-unique-validator
I had this issue and it was due to already existing duplicate values in my index (as pointed out in aheckmann’s comment on Feb 1, 2013). I was reading from here http://mongoosejs.com/docs/api.html#schematype_SchemaType-required where it mentions the error, but fails to mention about already exiting duplicates preventing the uniques index from being created in the first place. Might be worth updating the doc to mention this too!