问题
I keep getting the above mentioned error. I have deleted the existing db field id
which was set to unique. But on save I got the below exception
raise NotUniqueError(message % unicode(err))
NotUniqueError: Tried to save duplicate unique keys (E11000 duplicate key error index: test.users.$id_1 dup key: { : null })
My user
table looks like,
class Users(db.Document, UserMixin):
name = db.StringField(max_length=50)
email = db.StringField(max_length=255)
password = db.StringField(max_length=255)
city = db.StringField(max_length=125)
active = db.BooleanField(default=True)
company = db.StringField(max_length=255)
type = db.StringField(max_length=15)
confirmed_at = db.DateTimeField()
role = db.ReferenceField(Role)
meta = {'strict': False}
I also tried unsetting the id
attribute using,
cls.objects.update(**{"unset__id": 1})
But it throws this exception,
raise OperationError(u'Update failed (%s)' % unicode(err))
OperationError: Update failed (Mod on _id not allowed)
I just want to save the user model without id
field.
回答1:
If call list_indexes() it will show an unique index on the id
field.
You need to drop the unique index on the id
field in the collection as well using db.collection.dropIndex().
I am not sure if mongoengine provides a drop_index
class method but you can do this from the shell.
来源:https://stackoverflow.com/questions/40716486/notuniqueerror-tried-to-save-duplicate-unique-keys