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.
* 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>
Comment (by Teddy Ni):
Thank you for finding and linking to the duplicate ticket!
--
Ticket URL: <https://code.djangoproject.com/ticket/33330#comment:2>