[Django] #33330: Printing QuerySet.query from non-default DB can throw an error

40 views
Skip to first unread message

Django

unread,
Nov 29, 2021, 9:35:46 PM11/29/21
to django-...@googlegroups.com
#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.

Django

unread,
Nov 30, 2021, 12:08:21 AM11/30/21
to django-...@googlegroups.com
#33330: Printing QuerySet.query from non-default DB can throw an error
-------------------------------------+-------------------------------------
Reporter: Theodore Ni | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate
* component: Uncategorized => Database layer (models, ORM)
* type: Uncategorized => Bug


Comment:

Duplicate of #25947.

--
Ticket URL: <https://code.djangoproject.com/ticket/33330#comment:1>

Django

unread,
Nov 30, 2021, 5:27:14 PM11/30/21
to django-...@googlegroups.com
#33330: Printing QuerySet.query from non-default DB can throw an error
-------------------------------------+-------------------------------------
Reporter: Teddy Ni | Owner: nobody

Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Teddy Ni):

Thank you for finding and linking to the duplicate ticket!

--
Ticket URL: <https://code.djangoproject.com/ticket/33330#comment:2>

Reply all
Reply to author
Forward
0 new messages