Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#33330 closed Bug (duplicate)

Printing QuerySet.query from non-default DB can throw an error

Reported by: Teddy Ni Owned by: nobody
Component: Database layer (models, ORM) Version: 3.2
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (2)

comment:1 by Mariusz Felisiak, 3 years ago

Component: UncategorizedDatabase layer (models, ORM)
Resolution: duplicate
Status: newclosed
Type: UncategorizedBug

Duplicate of #25947.

comment:2 by Teddy Ni, 3 years ago

Thank you for finding and linking to the duplicate ticket!

Note: See TracTickets for help on using tickets.
Back to Top