djongo
djongo copied to clipboard
User.objects.filter(is_active=True) throws SQLDecodeError
Some model.objects.filter() queries cause a SQLDecodeError
BROKEN FILTER QUERY
from django.contrib.auth.models import User
User.objects.filter(is_active=True)
There is a work-around, which is to use the "__in" instead:
from django.contrib.auth.models import User
User.objects.filter(is_active__in=[True])
Traceback
Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/djongo/sql2mongo/query.py", line 856, in parse return handler(self, statement) File "/usr/local/lib/python3.6/dist-packages/djongo/sql2mongo/query.py", line 932, in _select return SelectQuery(self.db, self.connection_properties, sm, self._params) File "/usr/local/lib/python3.6/dist-packages/djongo/sql2mongo/query.py", line 116, in init super().init(*args) File "/usr/local/lib/python3.6/dist-packages/djongo/sql2mongo/query.py", line 62, in init self.parse() File "/usr/local/lib/python3.6/dist-packages/djongo/sql2mongo/query.py", line 152, in parse self.where = WhereConverter(self, statement) File "/usr/local/lib/python3.6/dist-packages/djongo/sql2mongo/converters.py", line 27, in init self.parse() File "/usr/local/lib/python3.6/dist-packages/djongo/sql2mongo/converters.py", line 122, in parse params=self.query.params File "/usr/local/lib/python3.6/dist-packages/djongo/sql2mongo/operators.py", line 475, in init self._statement2ops() File "/usr/local/lib/python3.6/dist-packages/djongo/sql2mongo/operators.py", line 438, in _statement2ops if prev_op.lhs is None: AttributeError: 'NoneType' object has no attribute 'lhs'
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/djongo/cursor.py", line 56, in execute params) File "/usr/local/lib/python3.6/dist-packages/djongo/sql2mongo/query.py", line 783, in init self._query = self.parse() File "/usr/local/lib/python3.6/dist-packages/djongo/sql2mongo/query.py", line 884, in parse raise exe from e djongo.exceptions.SQLDecodeError:
Keyword: None
Sub SQL: None
FAILED SQL: SELECT "auth_user"."_id", "auth_user"."password", "auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", "auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", "auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM "auth_user" WHERE "auth_user"."is_active" LIMIT 21
Params: ()
Version: 1.3.3
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/usr/local/lib/python3.6/dist-packages/djongo/cursor.py", line 59, in execute raise db_exe from e djongo.database.DatabaseError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/lib/python3.6/code.py", line 91, in runcode
exec(code, self.locals)
File "
Thanks a lot for the workaround.
Please check this out @nesdis
The workaround works but do we have any possible fix for the boolean assertion.
tutorials = Tutorial.objects.filter(published__in=[True])
# Works
tutorials = Tutorial.objects.filter(published=True)
# Failes
tutorials = Tutorial.objects.filter(published__in=[True])
# Workstutorials = Tutorial.objects.filter(published=True)
# Failes
Still in Nov 3 2022.
tutorials = Tutorial.objects.filter(published__in=[True])
# Workstutorials = Tutorial.objects.filter(published=True)
# Failes
Still works in Jan 2023. Thanks
I'm still facing the same problem with filtering boolean expressions. As far as I can see the problem is in converting the django code to SQL.
When you call boolean filter like:
User.objects.filter(is_active=True)
Generated this:
... from user where is_active
But actually it should be like this
... from user where is_active == true
@debu999's solution still works. This is because it converts the boolean equality wrong as above but converts the "is in" filter correct.
@debu999's solution still works Mar-24.
Products.objects.filter(active__in=[True])