Error with ReferenceField pointing to the abstract document
gerlv opened this issue · 0 comments
gerlv commented
We've run into an issue where reload
, save
, and update
throw an error when they dereference the abstract field from a ReferenceField.
It works fine when you initially save the object or get it from the DB but fails if you modify and then save it.
It also works if you use setattr
to set the attribute.
Since we use an abstract document, it doesn't exist in the database, so dereference method fails.
Traceback
>>> demo()
Company
456
changed to the second title
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/code/issue.py", line 55, in demo
issue.reload() # works
File "/app/venv/lib/python3.10/site-packages/mongoengine/document.py", line 745, in reload
self._qs.read_preference(ReadPreference.PRIMARY)
File "/app/venv/lib/python3.10/site-packages/mongoengine/queryset/base.py", line 844, in select_related
return queryset._dereference(queryset, max_depth=max_depth)
File "/app/venv/lib/python3.10/site-packages/mongoengine/dereference.py", line 102, in __call__
self.object_map = self._fetch_objects(doc_type=doc_type)
File "/app/venv/lib/python3.10/site-packages/mongoengine/dereference.py", line 197, in _fetch_objects
references = get_db()[collection].find({"_id": {"$in": refs}})
File "/app/venv/lib/python3.10/site-packages/pymongo/database.py", line 237, in __getitem__
return Collection(self, name)
File "/app/venv/lib/python3.10/site-packages/pymongo/collection.py", line 207, in __init__
raise TypeError("name must be an instance of str")
TypeError: name must be an instance of str
Example
from mongoengine import StringField, Document, ReferenceField
class Owner(Document):
meta = {"abstract": True}
name = StringField()
class Company(Owner):
# meta = {"strict": False}
key = StringField()
class Issue(Document):
title = StringField(required=True)
owner = ReferenceField(Owner) # since Owner is the abstract class; it is not registered in the database
def demo():
company = Company(name="Company", key="456")
company.save()
issue = Issue(title="image", owner=company).save()
print(issue.owner.name) # prints "Company"
print(issue.owner.key) # prints "456"
setattr(issue, "title", "second title")
issue.save()
print("changed to second title")
issue.reload() # fails
# also fails
issue.text = "another text"
issue.save() # fails