mongodb - mongoengine NotUniqueError on empty collection -
i trying save first object collection (i did db.connection.drop_database() ensure first object), keep getting *** notuniqueerror: tried save duplicate unique keys
error.
here's quick example of get:
ipdb> card.objects.all() [] ipdb> card `<card: <card url/pk: c569e1c9-7311-441e-ba03-0e86d4bc2932>>` ipdb> card.save() *** notuniqueerror: tried save duplicate unique keys (e11000 duplicate key error index: contacts.dav_object.$username_1 dup key: { : null }) ipdb> card.drop_collection() ipdb> card.save() `<card: <card url/pk: c569e1c9-7311-441e-ba03-0e86d4bc2932>>` ipdb>
i'm using unittest, , setup
method has card.ensure_indexes()
. suspect has it, without line mongo accept duplicated entries, if model definition says unique=true
.
if add card._collection.drop_indexes()
after card.ensure_indexes()
problem goes away, duplicated entries, said above.
here's card model:
class card(davobject): addressbook = db.referencefield("addressbook") url = db.stringfield(required=true, unique=true, unique_with=["addressbook", "url"]) active = db.booleanfield(default=true) text = db.stringfield(required=true)
and here's davobject:
class davobject(db.document): meta = { 'allow_inheritance': true, } last_update = db.complexdatetimefield(required=true, default=datetime.utcnow) def __unicode__(self): return u"<%s url/pk: %s>" % (self.__class__.__name__, getattr(self, "url", self.pk)) def save(self, *args, **kwargs): if self._get_changed_fields(): self.last_update = datetime.utcnow() return db.document.save(self, *args, **kwargs)
i can't figure out what's wrong.
i'm using mongoengine 0.8.3, flask 0.10.1 , python 2.7.3.
look problem inheritance: set 'allow_inheritance': true
davobject
means inherited models store in 1 collection.
you have username
index model inherited davobject
, have conflict when trying insert 2 inherited davobject
documents same username
(none).
if not shure need use 'allow_inheritance': true
set false
or try put documents different indexes separate collections.
to check assumption try print list(davobject.objects.all())
.
Comments
Post a Comment