MDK
MDK

Reputation: 690

duplicate key error index in mongoengine

I have a Page model as follow and the page_id field isn't unique but I can't create an instance from Page and this error rises
OperationError: Could not save document (insertDocument :: caused by ::11000 E11000 duplicate key error index: shopify.page.$pageId_1 dup key: { : null }) I have no idea that why the duplicate key error rises when the page_id field isn't unique

The Site model :

class Page(Document):

    # page identity
    page_id = StringField()
    store = ReferenceField('Store')
    is_product = BooleanField(default = False)
    is_homepage = BooleanField(default = False)
    product = ReferenceField('Product')
    requests = ListField(EmbeddedDocumentField('Request'))

    # page stat
    visitors = IntField(default = 0)
    views = IntField(default = 0)
    past_days = ListField(EmbeddedDocumentField('DayStat'))

Upvotes: 1

Views: 2088

Answers (2)

Roshan Yadav
Roshan Yadav

Reputation: 143

Yes,I also removed the unique=True from the field attribute, and after that dropped the Collection and ran the code again. This resolved the issue.

Upvotes: 1

MDK
MDK

Reputation: 690

it's just because of some old Document in database that in past have different fields and when the new changes applied because of different objects type in Collection when a function running it unable to recognize the type of object of field and go down !
one solution is dropping the Collection and if your Collection is too important you can write a script that delete old objects and make a new instance from them.

Upvotes: 4

Related Questions