You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This bug has arisen from changing the db_field of a value due to the new schema of that value having validation errors, in an attempt to migrate the data.
It seems that if the document does not have the key db_field it will fall back to the field name still, which is somewhat unexpected behaviour and can cause unexpected validation errors.
As this behaviour is not described in the documentation at all, it should at least be put in as warnings if not resolved.
The below code reproduces the bug described.
frommongoengineimport*classTestDocument(Document):
attributeName=StringField(db_field="fieldName")
if__name__=='__main__':
db=connect('Issue#1102')
db.drop_database('Issue#1102')
# Create Test Documentd=TestDocument()
d.save()
# get and validate objectd2=TestDocument.objects().first()
# VALIDd2.validate()
# Add in bad fieldTestDocument.objects.update(__raw__={
"$set": {
"attributeName": 1
}
})
# get and validate objectd3=TestDocument.objects().first()
# FAILStry:
d3.validate()
exceptValidationErrorase:
print("Failed d3.validate(). %s"%e)
# Add in good fieldTestDocument.objects.update(__raw__={
"$set": {
"fieldName": "string"
}
})
# get and validate objectd4=TestDocument.objects().first()
# VALIDd4.validate()
The text was updated successfully, but these errors were encountered:
touilleMan
added a commit
to touilleMan/mongoengine
that referenced
this issue
Sep 21, 2015
Given _from_son create an a Document (hence using __init__), the behavior of the field renaming is changing (depending only __init__ is called or _from_son + __init__. This makes the fix of this bug a bit tricky...
I just replicated this behavior in v0.11.0. Still a bug:
In [47]: class TestDocument(Document):
attributeName = StringField(db_field="fieldName")
....:
In [48]: TestDocument.drop_collection()
In [49]: TestDocument._get_collection().insert({'attributeName': 1}) # equal to the field name, but not the db_field
Out[49]: ObjectId('586344e7c6e47b16a6b606d6')
In [50]: TestDocument._get_collection().find_one()
Out[50]: {u'_id': ObjectId('586344e7c6e47b16a6b606d6'), u'attributeName': 1}
In [51]: doc = TestDocument.objects.first()
In [52]: doc._data
Out[52]: {"id": ObjectId('586344e7c6e47b16a6b606d6'), "attributeName": 1}
In [53]: doc.attributeName # this should be None
Out[53]: 1
This bug has arisen from changing the
db_field
of a value due to the new schema of that value having validation errors, in an attempt to migrate the data.It seems that if the document does not have the key
db_field
it will fall back to the field name still, which is somewhat unexpected behaviour and can cause unexpected validation errors.As this behaviour is not described in the documentation at all, it should at least be put in as warnings if not resolved.
The below code reproduces the bug described.
The text was updated successfully, but these errors were encountered: