#26061: Empty queryset raises EmptyResultSet  (instead of print SQL query)
-------------------------------------+-------------------------------------
     Reporter:  Adam Dobrawy         |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  3.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  EmptyResultSet       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by tsyplakou):

 * version:  1.8 => 3.1


Old description:

> {{{
> In [19]: import django
>
> In [20]: django.VERSION
> Out[20]: (1, 8, 7, 'final', 0)
>
> In [21]: print Record.objects.filter(pk__in=[])
> []
> In [22]: print Record.objects.filter(pk__in=[]).query
> ---------------------------------------------------------------------------
> EmptyResultSet                            Traceback (most recent call
> last)
> <ipython-input-22-e81ae73ee15f> in <module>()
> ----> 1 print Record.objects.filter(pk__in=[]).query
>
> /home/adas/.virtualenvs/poradnia/local/lib/python2.7/site-
> packages/django/db/models/sql/query.pyc in __str__(self)
>     213         done by the database interface at execution time.
>     214         """
> --> 215         sql, params = self.sql_with_params()
>     216         return sql % params
>     217
>
> /home/adas/.virtualenvs/poradnia/local/lib/python2.7/site-
> packages/django/db/models/sql/query.pyc in sql_with_params(self)
>     221         substituted into the query.
>     222         """
> --> 223         return self.get_compiler(DEFAULT_DB_ALIAS).as_sql()
>     224
>     225     def __deepcopy__(self, memo):
>
> /home/adas/.virtualenvs/poradnia/local/lib/python2.7/site-
> packages/django/db/models/sql/compiler.pyc in as_sql(self, with_limits,
> with_col_aliases, subquery)
>     385             from_, f_params = self.get_from_clause()
>     386
> --> 387             where, w_params = self.compile(self.query.where)
>     388             having, h_params = self.compile(self.query.having)
>     389             params = []
>
> /home/adas/.virtualenvs/poradnia/local/lib/python2.7/site-
> packages/django/db/models/sql/compiler.pyc in compile(self, node,
> select_format)
>     355             sql, params = vendor_impl(self, self.connection)
>     356         else:
> --> 357             sql, params = node.as_sql(self, self.connection)
>     358         if select_format and not self.subquery:
>     359             return node.output_field.select_format(self, sql,
> params)
>
> /home/adas/.virtualenvs/poradnia/local/lib/python2.7/site-
> packages/django/db/models/sql/where.pyc in as_sql(self, compiler,
> connection)
>     131                     return '', []
>     132                 else:
> --> 133                     raise EmptyResultSet
>     134             if full_needed - everything_childs <= 0:
>     135                 if self.negated:
>
> EmptyResultSet:
>
> }}}
>
> I expect to achieve SQL query not exception.
>
> There is #10181, but it was about query execution. My issues is about
> query printing.

New description:

 {{{
 Internal Server Error: /api/travel/places/
 Traceback (most recent call last):
   File "/Users/User/Projects/travel_envs/back_env/lib/python3.8/site-
 packages/django/core/handlers/exception.py", line 47, in inner
     response = get_response(request)
   File "/Users/User/Projects/travel_envs/back_env/lib/python3.8/site-
 packages/django/core/handlers/base.py", line 181, in _get_response
     response = wrapped_callback(request, *callback_args,
 **callback_kwargs)
   File "/Users/User/Projects/travel_envs/back_env/lib/python3.8/site-
 packages/django/views/decorators/csrf.py", line 54, in wrapped_view
     return view_func(*args, **kwargs)
   File "/Users/User/Projects/travel_envs/back_env/lib/python3.8/site-
 packages/rest_framework/viewsets.py", line 125, in view
     return self.dispatch(request, *args, **kwargs)
   File "/Users/User/Projects/travel_envs/back_env/lib/python3.8/site-
 packages/rest_framework/views.py", line 509, in dispatch
     response = self.handle_exception(exc)
   File "/Users/User/Projects/travel_envs/back_env/lib/python3.8/site-
 packages/rest_framework/views.py", line 469, in handle_exception
     self.raise_uncaught_exception(exc)
   File "/Users/User/Projects/travel_envs/back_env/lib/python3.8/site-
 packages/rest_framework/views.py", line 480, in raise_uncaught_exception
     raise exc
   File "/Users/User/Projects/travel_envs/back_env/lib/python3.8/site-
 packages/rest_framework/views.py", line 506, in dispatch
     response = handler(request, *args, **kwargs)
   File "/Users/User/Projects/travel/travel_backend/travel/views.py", line
 2141, in export
     sql, params = queryset.none().query.sql_with_params()
   File "/Users/User/Projects/travel_envs/back_env/lib/python3.8/site-
 packages/django/db/models/sql/query.py", line 264, in sql_with_params
     return self.get_compiler(DEFAULT_DB_ALIAS).as_sql()
   File "/Users/User/Projects/travel_envs/back_env/lib/python3.8/site-
 packages/django/db/models/sql/compiler.py", line 513, in as_sql
     where, w_params = self.compile(self.where) if self.where is not None
 else ("", [])
   File "/Users/User/Projects/travel_envs/back_env/lib/python3.8/site-
 packages/django/db/models/sql/compiler.py", line 430, in compile
     sql, params = node.as_sql(self, self.connection)
   File "/Users/User/Projects/travel_envs/back_env/lib/python3.8/site-
 packages/django/db/models/sql/where.py", line 99, in as_sql
     raise EmptyResultSet
 django.core.exceptions.EmptyResultSet
 }}}

 **AnyModel.objects.none().query.sql_with_params()** raises
 **django.core.exceptions.EmptyResultSet**

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/26061#comment:4>
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/062.5c48124af6861a0046c5557a76a6f3f5%40djangoproject.com.

Reply via email to