MongoEngine / MongoEngine/mongoengine

Problem dereferencing non-binary UUID primary keys

Open
#915 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting Response Bug Discussion
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.