djongo icon indicating copy to clipboard operation
djongo copied to clipboard

User.objects.filter(is_active=True) throws SQLDecodeError

Open elliotsyoung opened this issue 3 years ago • 7 comments

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 "", line 1, in File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 263, in repr data = list(self[:REPR_OUTPUT_SIZE + 1]) File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 287, in iter self._fetch_all() File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 1303, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "/usr/local/lib/python3.6/dist-packages/django/db/models/query.py", line 53, in iter results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size) File "/usr/local/lib/python3.6/dist-packages/django/db/models/sql/compiler.py", line 1154, in execute_sql cursor.execute(sql, params) File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 98, in execute return super().execute(sql, params) File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 66, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers return executor(sql, params, many, context) 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/django/db/utils.py", line 90, in exit raise dj_exc_value.with_traceback(traceback) from exc_value 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 django.db.utils.DatabaseError

elliotsyoung avatar Aug 28 '20 21:08 elliotsyoung

Thanks a lot for the workaround.

Please check this out @nesdis

ronpetit avatar Nov 06 '20 23:11 ronpetit

The workaround works but do we have any possible fix for the boolean assertion.

debu999 avatar Nov 05 '21 04:11 debu999

tutorials = Tutorial.objects.filter(published__in=[True]) # Works tutorials = Tutorial.objects.filter(published=True) # Failes

debu999 avatar Nov 05 '21 04:11 debu999

tutorials = Tutorial.objects.filter(published__in=[True]) # Works tutorials = Tutorial.objects.filter(published=True) # Failes

Still in Nov 3 2022.

bnisevic avatar Nov 02 '22 23:11 bnisevic

tutorials = Tutorial.objects.filter(published__in=[True]) # Works tutorials = Tutorial.objects.filter(published=True) # Failes

Still works in Jan 2023. Thanks

feyton avatar Jan 07 '23 15:01 feyton

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.

YusufBerki avatar Feb 02 '23 19:02 YusufBerki

@debu999's solution still works Mar-24. Products.objects.filter(active__in=[True])

algiraldohe avatar Mar 31 '24 14:03 algiraldohe