#33330: Printing QuerySet.query from non-default DB can throw an error
-----------------------------------------+------------------------
Reporter: tjni | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
As a new user of Django who needed to know more about the SQL that the ORM
was using, I found some suggestions on the web to print and inspect the
"query" field of a QuerySet.
The app that I work on has a non-default database that uses
django.contrib.gis.db.backends.postgis as its backend, and it turns out
that an error can occur when printing "query" from a QuerySet that will be
routed to this backend:
{{{
File "/home/tni/code/myproject/myproject_maps/views.py", line 205, in
retrieve
print(f"{test_query_set.query}")
File "/home/tni/code/myproject/env/lib/python3.9/site-
packages/django/db/models/sql/query.py", line 256, in __str__
sql, params = self.sql_with_params()
File "/home/tni/code/myproject/env/lib/python3.9/site-
packages/django/db/models/sql/query.py", line 264, in sql_with_params
return self.get_compiler(DEFAULT_DB_ALIAS).as_sql()
File "/home/tni/code/myproject/env/lib/python3.9/site-
packages/django/db/models/sql/compiler.py", line 498, in as_sql
extra_select, order_by, group_by = self.pre_sql_setup()
File "/home/tni/code/myproject/env/lib/python3.9/site-
packages/django/db/models/sql/compiler.py", line 55, in pre_sql_setup
self.setup_query()
File "/home/tni/code/myproject/env/lib/python3.9/site-
packages/django/db/models/sql/compiler.py", line 46, in setup_query
self.select, self.klass_info, self.annotation_col_map =
self.get_select()
File "/home/tni/code/myproject/env/lib/python3.9/site-
packages/django/db/models/sql/compiler.py", line 267, in get_select
sql, params = col.select_format(self, sql, params)
File "/home/tni/code/myproject/env/lib/python3.9/site-
packages/django/db/models/expressions.py", line 387, in select_format
return self.output_field.select_format(compiler, sql, params)
File "/home/tni/code/myproject/env/lib/python3.9/site-
packages/django/contrib/gis/db/models/fields.py", line 276, in
select_format
return compiler.connection.ops.select % sql, params
AttributeError: 'DatabaseOperations' object has no attribute 'select'
}}}
It appears that the type of connection matters, and an initial look into
the code suggests that the line:
{{{
return self.get_compiler(DEFAULT_DB_ALIAS).as_sql()
}}}
on line 264 of django/db/models/sql/query.py should use the connection of
its containing QuerySet.
I wanted to file this ticket to hear a more informed opinion about this
analysis. If it is a mistake, I do not mind contributing a patch to fix
it, following guidance.
Thank you to whoever reads this for your time.
--
Ticket URL: <https://code.djangoproject.com/ticket/33330>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/047.ae7ed398f1a46db4ca221f55ae95f308%40djangoproject.com.