Open
Description
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.
from mongoengine import *
class TestDocument(Document):
attributeName = StringField(db_field="fieldName")
if __name__ == '__main__':
db = connect('Issue#1102')
db.drop_database('Issue#1102')
# Create Test Document
d = TestDocument()
d.save()
# get and validate object
d2 = TestDocument.objects().first()
# VALID
d2.validate()
# Add in bad field
TestDocument.objects.update(__raw__={
"$set": {
"attributeName": 1
}
})
# get and validate object
d3 = TestDocument.objects().first()
# FAILS
try:
d3.validate()
except ValidationError as e:
print("Failed d3.validate(). %s" % e)
# Add in good field
TestDocument.objects.update(__raw__={
"$set": {
"fieldName": "string"
}
})
# get and validate object
d4 = TestDocument.objects().first()
# VALID
d4.validate()