MongoEngine / MongoEngine/mongoengine
The id type of full object and the id type of the lazy object are different
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 4.3k
- Forks
- 1.2k
- Avg merge
- 4h 41m
- Merged PRs (30d)
- 11
Description
- Models:
import datetime
import uuid
from mongoengine import *
class Model1(Document):
id = UUIDField(primary_key=True, default=uuid.uuid4, binary=False)
attr1 = StringField(required=True)
attr2 = IntField()
attr3 = BooleanField()
creation_date = DateTimeField(default=datetime.datetime.utcnow)
meta = {
'db_alias': 'testdb',
'collection': 'model1'
}
class Model2(Document):
id = UUIDField(primary_key=True, default=uuid.uuid4, binary=False)
attr1 = StringField()
reference_field = LazyReferenceField('Model1', dbref=True)
creation_date = DateTimeField(default=datetime.datetime.utcnow)
meta = {
'db_alias': 'testdb',
'collection': 'model2'
}
- Insert sample data (NOTE: the Earth object reference the Moon object):
moon = Model1()
moon.attr1 = 'Moon'
moon.attr2 = 22
moon.attr3 = False
moon.save()
earth = Model2()
earth.attr1 = 'Earth'
earth.reference_field = moon
earth.save()
- If I check that the Earth object references the moon object by comparing ids, the result is that it does not
earth = Model2.objects.get(attr1='Earth')
moon = Model1.objects.get(attr1='Moon')
if earth.reference_field.id == moon.id:
print('The Earth reference to the Moon')
else:
print('The Earth does NOT reference to the Moon')
# OUTPUT: The Earth does NOT reference to the Moon <-- ¿WTF?
- The cause is that the id type of full object is UUID and the id type of the lazy object is string:
print('Types ids:', type(moon.id), type(earth.reference_field.id))
# OUTPUT: Types ids: <class 'uuid.UUID'> <class 'str'> <-- the ids have different types!!!!
I can't use LazyReference objects if I can't easily compare ids without doing conversions.
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
Reproduce the mismatch with the Model1 and Model2 definitions and sample queries shown in the issue. Start by tracing how LazyReferenceField loads its id versus how UUIDField returns the full object's id. Done means the lazy and full references expose compatible UUID ids and a regression test covers the comparison.
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
- 38/100