Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid embedded document instance provided to an EmbeddedDocumentField on save

I have these mongoengine models declared:

class SyncDiscrepancy(EmbeddedDocument):
    upi = StringField(primary_key=True)
    error_code = IntField(required=True)

    meta = {
        'indexes': ['upi', 'error_code']
    }
########## END SYNC


class Flight(Document):
    identifier = StringField(primary_key=True)
    env = StringField(required=True, max_length=3)
    peak = IntField(required=True)
    carrier = StringField(required=True, max_length=3)
    number = IntField(required=True)
    boardpoint = StringField(required=True)
    offpoint = StringField(required=True)
    date = DateTimeField(required=True)
    status = StringField(required=True)
    # store comments
    comments = StringField()

    last_modified = DateTimeField(required=True)

    local_discrepancies = ListField(EmbeddedDocumentField(LocalDiscrepancy))
    sync_discrepancies = ListField(EmbeddedDocumentField(SyncDiscrepancy))
    count_local = IntField(required=True)
    count_sync = IntField(required=True)

    meta = {
        'indexes': ['_id', 'env','peak', 'date'],
        'ordering': ['-date']
    }

And I try a basic

>>> sy = SyncDiscrepancy(upi='axzdsa', error_code=2)
>>> fl = Flight()
>>> fl.sync_discrepancies.append(sy)
>>> fl.save()
Traceback (most recent call last):

File "<debugger>", line 1, in <module>

fl.save()

File "/usr/lib/python2.7/site-packages/mongoengine/document.py", line 224, in save

self.validate(clean=clean)

File "/usr/lib/python2.7/site-packages/mongoengine/base/document.py", line 323, in validate

raise ValidationError(message, errors=errors)

ValidationError: ValidationError (Flight:None) (Invalid embedded document instance provided to an EmbeddedDocumentField: ['sync_discrepancies'] Field is required: ['status', 'count_local', 'offpoint', 'identifier', 'number', 'boardpoint', 'last_modified', 'peak', 'env', 'carrier', 'date', 'count_sync'])

Now I know that I didn't fill the required fields for flight, but even if I do, I still get this error Invalid embedded document instance provided to an EmbeddedDocumentField: ['sync_discrepancies']. What exactly is the problem with how I declare the SyncDiscrepancy and save the flight??

Using mongoengine==0.8.7

EDIT Shortened it to

class SyncDiscrepancy(EmbeddedDocument):
    error_code = IntField()

class Flight(Document):
    sync_discrepancies = ListField(EmbeddedDocumentField(SyncDiscrepancy))

and:

>>> sy = SyncDiscrepancy(error_code=2)
>>> fl = Flight()
>>> fl.sync_discrepancies.append(fl)
>>> fl.save()
Traceback (most recent call last):

File "<debugger>", line 1, in <module>

fl.save()

File "/usr/lib/python2.7/site-packages/mongoengine/document.py", line 224, in save

self.validate(clean=clean)

File "/usr/lib/python2.7/site-packages/mongoengine/base/document.py", line 323, in validate

raise ValidationError(message, errors=errors)

ValidationError: ValidationError (Flight:None) (Invalid embedded document instance provided to an EmbeddedDocumentField: ['sync_discrepancies'])

>>> 

I don't understand why I get this error.

like image 769
confused00 Avatar asked Jan 08 '15 17:01

confused00


2 Answers

I ended up switching the order of the model declarations and it just worked (i.e. declaring the model after being referenced (???))

class Flight(Document):
    sync_discrepancies = ListField(EmbeddedDocumentField('SyncDiscrepancy'))

class SyncDiscrepancy(EmbeddedDocument):
    error_code = IntField()
like image 72
confused00 Avatar answered Nov 13 '22 13:11

confused00


I was going crazy with this error and in my case embedded document classes where referenced by name (using a quote ' notation) and removing the quotes fixed the issue.

I had something like this:

class SyncDiscrepancy(EmbeddedDocument):
    error_code = IntField()

class Flight(Document):
    sync_discrepancies = ListField(EmbeddedDocumentField('SyncDiscrepancy'))

Changing it to this

class SyncDiscrepancy(EmbeddedDocument):
    error_code = IntField()

class Flight(Document):
    sync_discrepancies = ListField(EmbeddedDocumentField(SyncDiscrepancy))

fixed the issue.

like image 1
Vincent de Lagabbe Avatar answered Nov 13 '22 14:11

Vincent de Lagabbe