MongoEngine / MongoEngine/mongoengine
db_field isn't used if field is not present in document, leading to validation errors
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
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()
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided Python reproducer against MongoEngine and compare validation when the document contains attributeName versus its db_field, fieldName. Trace the loading and validation path for db_field handling; done means the reproduced case no longer produces the unexpected validation error, or the fallback behavior is explicitly warned about and documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100