-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
regression from 0.9.0: assert on reverse_delete_rule=CASCADE with source.id.field != dest.id.field #1135
Labels
Comments
alon
changed the title
regression from 0.9.0: reverse_delete_rule=CASCADE with non ObjectId id field fails
regression from 0.9.0: assert on reverse_delete_rule=CASCADE with source.id.field != dest.id.field
Oct 27, 2015
alon
added a commit
to ConsumerPhysics/mongoengine
that referenced
this issue
Oct 27, 2015
alon
added a commit
to ConsumerPhysics/mongoengine
that referenced
this issue
Oct 27, 2015
Seems like related to this bug one more case. class User(Document):
"""Collection of users profiles."""
email = EmailField(required=True, unique=True)
username = StringField(regex=r'[a-zA-Z0-9_-]+$', max_length=120,
required=True, unique=True)
password = StringField(max_length=64, required=True)
is_superuser = BooleanField(default=False)
is_disabled = BooleanField(default=False)
meta = {
'collection': 'users',
'db_alias': 'makechat_test' if TEST_MODE else 'makechat',
'indexes': ['email', 'username', 'password']
}
def __str__(self):
"""Standart python magic __str__ method."""
return self.username
class Session(Document):
"""Collection of users sessions."""
user = ReferenceField(User, reverse_delete_rule=CASCADE)
created = DateTimeField(default=datetime.now)
value = StringField(max_length=64, primary_key=True)
meta = {
'collection': 'sessions',
'db_alias': 'makechat_test' if TEST_MODE else 'makechat',
'indexes': [
{'fields': ['created'], 'expireAfterSeconds': SESSION_TTL}
]
} then >>> from makechat.models import User
>>> User.objects.all().delete()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/buran/envs/py3/lib/python3.4/site-packages/mongoengine/queryset/base.py", line 409, in delete
ref_q_count = ref_q.count()
File "/home/buran/envs/py3/lib/python3.4/site-packages/mongoengine/queryset/queryset.py", line 104, in count
return super(QuerySet, self).count(with_limit_and_skip)
File "/home/buran/envs/py3/lib/python3.4/site-packages/mongoengine/queryset/base.py", line 347, in count
return self._cursor.count(with_limit_and_skip=with_limit_and_skip)
File "/home/buran/envs/py3/lib/python3.4/site-packages/mongoengine/queryset/base.py", line 1481, in _cursor
self._cursor_obj = self._collection.find(self._query,
File "/home/buran/envs/py3/lib/python3.4/site-packages/mongoengine/queryset/base.py", line 1515, in _query
self._mongo_query = self._query_obj.to_query(self._document)
File "/home/buran/envs/py3/lib/python3.4/site-packages/mongoengine/queryset/visitor.py", line 90, in to_query
query = query.accept(QueryCompilerVisitor(document))
File "/home/buran/envs/py3/lib/python3.4/site-packages/mongoengine/queryset/visitor.py", line 155, in accept
return visitor.visit_query(self)
File "/home/buran/envs/py3/lib/python3.4/site-packages/mongoengine/queryset/visitor.py", line 78, in visit_query
return transform.query(self.document, **query.query)
File "/home/buran/envs/py3/lib/python3.4/site-packages/mongoengine/queryset/transform.py", line 61, in query
raise InvalidQueryError(e)
mongoengine.errors.InvalidQueryError: Cannot resolve field "id"
Cannot resolve field "id" but changing name to class Session(Document):
"""Collection of users sessions."""
user = ReferenceField(User, reverse_delete_rule=CASCADE)
created = DateTimeField(default=datetime.now)
id = StringField(max_length=64, primary_key=True)
meta = {
'collection': 'sessions',
'db_alias': 'makechat_test' if TEST_MODE else 'makechat',
'indexes': [
{'fields': ['created'], 'expireAfterSeconds': SESSION_TTL}
]
} >>> from makechat.models import User
>>> User.objects.all().delete()
0
>>> Python 3.4.3 |
Can also confirm this issue....
|
This is fixed now, sorry it took that long. |
(this is also a dupe of #1224) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The regression seems to be in queryset/base.py BaseQuerySet.delete, in creating the queryset in 0.9.0 there is only one clause, but in 0.10.0 there is an additional 'id__nin' clause that implicitly assumes the id of both objects is of the same field, and breaks when they are not, as in this example.
Additionally it doesn't make sense to check for id of a different collection not in this collection.
The following example breaks in 0.10.0 but works in 0.9.0:
from mongoengine import connect, Document, StringField, ReferenceField, CASCADE
The error in 0.10.0 results from the 'id__nin' clause on the rule's queryset that is checking the id of Token, which is ObjectId, but should be checking the id of Client, which is a StringField:
The query in 0.9.0:
ref_q = document_cls.objects(**{field_name + '__in': self})
in 0.10.0:
ref_q = document_cls.objects(**{field_name + '__in': self, 'id__nin': cascade_refs})
The text was updated successfully, but these errors were encountered: