Opened 4 years ago

Closed 3 years ago

#22973 closed Bug (wontfix)

cannot get SQL code of empty queryset

Reported by: emanuele.paolini@… Owned by: Anderson Resende
Component: Database layer (models, ORM) Version: 1.5
Severity: Normal Keywords:
Cc: Tim Graham Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Suppose you have defined any Model class.

The code:


raises an exception:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/", line 174, in __str__
    sql, params = self.sql_with_params()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/", line 182, in sql_with_params
    return self.get_compiler(DEFAULT_DB_ALIAS).as_sql()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/", line 85, in as_sql
    where, w_params = self.query.where.as_sql(qn=qn, connection=self.connection)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/", line 126, in as_sql
    raise EmptyResultSet

instead this works fine:


Change History (3)

comment:1 Changed 4 years ago by Tim Graham

Cc: Tim Graham added
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

Note that your second version works on 1.5, but also raises EmptyResultSet starting in 1.6. I don't think this change was intentional, although I think the pattern you presented isn't treated as an official API for retrieving SQL for a queryset and I'm not sure what would be involved in fixing it, so perhaps someone else may close this as "won't fix".

comment:2 Changed 3 years ago by Anderson Resende

Owner: changed from nobody to Anderson Resende
Status: newassigned

comment:3 Changed 3 years ago by Tim Graham

Resolution: wontfix
Status: assignedclosed

My understanding is that EmptyResultSet is raised when valid SQL cannot be generated (see #19263 for an example). Therefore I'm going to mark this as "won't fix".

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