So,
I have a fairly simple model which constrains on a particular owner only having a monkey with one name:
class Monkey(mongoengine.Document):
name = mongoengine.StringField(unique_with='owner')
owner = mongoengine.ReferenceField(User)
This works fine to enforce the constraint, however I then have issues in the following situation. When I try to insert a second Monkey for the same user with the same name, I get a NotUniqueError, however this gives me a broad message, and doesn't enable me to analyse which field raised the error
NotUniqueError: Tried to save duplicate unique keys (E11000 duplicate key error index: test.feed.$name_1_owner_1 dup key: { : "Bob", : { $ref: "user", $id: ObjectId('5145b8a28dd4ff1c5281d151') } })
I can of course parse the error message for the offending issue, discovering that test.feed.$name_1_owner_1 was the issue, and then pass on a nicer error message to the client, however this seems quite clunky. Is there a way to catch this exception on a per-field basis?
Thanks!