MongoEngine / MongoEngine/mongoengine
Problem dereferencing non-binary UUID primary keys
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
Hi all,
I have encountered a problem dereferencing instances when that have a primary key that is a UUID that is stored in mongodb as a string.
class Pool(db.Document):
uuid = db.UUIDField(required=True, primary_key=True, binary=False)
class Host(db.Document):
uuid = db.UUIDField(required=True, primary_key=True, binary=False )
pool = db.ReferenceField(Pool)
When I try to access to pool attribute I get a DBRef instance instead a Pool instance.
p = Pool(uuid="36a36f7b-54b7-724f-d28d-2262a629c18a")
h = Host(uuid="70a36f7b-1db7-724f-d28d-2262a629c36b")
h.pool = p
h.save()
host = Host.objects.get(uuid="70a36f7b-1db7-724f-d28d-2262a629c36b")
host.pool
DBRef('pool', UUID('36a36f7b-54b7-724f-d28d-2262a629c18a'))
The DBRef contains the UUID object but the value is stored as string in mongodb. So when this is used in the get of ReferenceField class the search is done using the UUID object instead of the string:
if self._auto_dereference and isinstance(value, DBRef):
value = self.document_type._get_db().dereference(value)
end return None
If the uuid is stored using binary=True all works fine.
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 ReferenceField.get, where the issue shows DBRef dereferencing, and trace how UUIDField values with binary=False are stored and looked up. Reproduce the example with string-backed UUID primary keys and compare it with binary=True. Done means accessing host.pool returns the Pool instance for non-binary UUID keys rather than a DBRef.
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
- 42/100