Skip to content

obj.exists() - SQLDecode Error, can't work with DRF. #659

@dinesh-hills

Description

@dinesh-hills

One line description of the issue

.exists method blows up django app, and this happens a lot when DRF validator execute def qs_exists() to check for unique constraints.

Python script

>>> Users.objects.filter(pk=1).exists()

Traceback

Traceback (most recent call last):
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 808, in iter
yield from iter(self._query)
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 167, in iter
yield self._align_results(doc)
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 269, in _align_results
if selected.table == self.left_table:
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/djongo/sql2mongo/sql_tokens.py", line 133, in table
return alias2token[name].table
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/djongo/sql2mongo/sql_tokens.py", line 133, in table
return alias2token[name].table
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/djongo/sql2mongo/sql_tokens.py", line 133, in table
return alias2token[name].table
[Previous line repeated 957 more times]
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/djongo/sql2mongo/sql_tokens.py", line 130, in table
name = self.given_table
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/djongo/sql2mongo/sql_tokens.py", line 141, in given_table
name = self._token.get_real_name()
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/sqlparse/sql.py", line 361, in get_real_name
return self._get_first_name(dot_idx)
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/sqlparse/sql.py", line 386, in _get_first_name
return token.get_name()
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/sqlparse/sql.py", line 355, in get_name
return self.get_alias() or self.get_real_name()
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/sqlparse/sql.py", line 344, in get_alias
_, ws = self.token_next_by(t=T.Whitespace)
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/sqlparse/sql.py", line 244, in token_next_by
return self._token_matching(funcs, idx, end)
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/sqlparse/sql.py", line 223, in _token_matching
if func(token):
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/sqlparse/sql.py", line 242, in
funcs = lambda tk: imt(tk, i, m, t)
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/sqlparse/utils.py", line 100, in imt
elif types and any(token.ttype in ttype for ttype in types):
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/sqlparse/utils.py", line 100, in
elif types and any(token.ttype in ttype for ttype in types):
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/sqlparse/tokens.py", line 19, in contains
return item is not None and (self is item or item[:len(self)] == self)
RecursionError: maximum recursion depth exceeded in comparison

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/djongo/cursor.py", line 76, in fetchone
return self.result.next()
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 797, in next
result = next(self._result_generator)
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/djongo/sql2mongo/query.py", line 830, in iter
raise exe from e
djongo.exceptions.SQLDecodeError:

Keyword: FAILED SQL: SELECT %(0)s AS "a" FROM "org_organization" WHERE "org_organization"."id" = %(1)s LIMIT 1

Params: (1, 1)
Version: 1.3.6
Sub SQL: None
FAILED SQL: None
Params: None
Version: None

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/django/db/utils.py", line 98, in inner
return func(*args, **kwargs)
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/djongo/cursor.py", line 81, in fetchone
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 "", line 1, in
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/django/db/models/query.py", line 1226, in exists
return self.query.has_results(using=self.db)
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/django/db/models/sql/query.py", line 592, in has_results
return compiler.has_results()
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1366, in has_results
return bool(self.execute_sql(SINGLE))
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/django/db/models/sql/compiler.py", line 1409, in execute_sql
val = cursor.fetchone()
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/django/db/utils.py", line 97, in inner
with self:
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/django/db/utils.py", line 91, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/django/db/utils.py", line 98, in inner
return func(*args, **kwargs)
File "/home/dinesh/.local/share/virtualenvs/ProjectHQ-CRbS596i/lib/python3.10/site-packages/djongo/cursor.py", line 81, in fetchone
raise db_exe from e
django.db.utils.DatabaseError

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions