MongoEngine / MongoEngine/mongoengine
`id` alias overrides db_field resulting in inaccessible data
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
Fields cannot use db_field='id'. When the resulting data is accessed, the found object is clobbered by the primary key.
Here I set up a simple example where I attempt to store my_id in mongo under the id field name.
from mongoengine import *
connect('test_db')
class MyObject(Document):
my_id = StringField(db_field='id')
obj = MyObject(my_id='obj_1')
obj.save()
It appears to work here as expected:
>>> print obj.id
553fe1afe138235f8f252c92
>>> print obj.my_id
obj_1
It is also correctly stored in mongo.
>>> db.my_object.find()
{ "_id" : ObjectId("553fe1afe138235f8f252c92"), "id" : "obj_1" }
But the data cannot be retrieved using Mongoengine:
>>> found = MyObject.objects.first()
>>> print found.id
None
>>> print found.my_id
553fe1afe138235f8f252c92
>>> print found.to_json()
{"id": {"$oid": "553fe96be13823608b528766"}}
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 with the MyObject example and reproduce the mismatch through MyObject.objects.first(), found.id, found.my_id, and found.to_json(). Trace how the db_field='id' mapping is loaded and serialized, then verify that a saved object round-trips without clobbering either field and that the serialized data remains accessible.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb, python
- Domain
- database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100