#26433: Bug: annotating using When(id__in=[], ...) returns empty queryset
-------------------------------------+-------------------------------------
     Reporter:  kevinmarsh           |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.9
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by akaariai):

 * needs_better_patch:   => 0
 * needs_docs:   => 0
 * needs_tests:   => 0
 * stage:  Unreviewed => Accepted


Comment:

 This is close to the problem mentioned in #26430. Here Case.as_sql() could
 just catch the EmptyResultSet and instead return sql "1 = 0". But I wonder
 if we are able to avoid the EmptyResultSet completely by the following
 approach.
 1. Instead of raising EmptyResultSet we generate sql, params = "1 = %s",
 (EMPTY_PARAM,).
 2. EMPTY_PARAM is converted to 0 when the SQL is executed.
 3. When generating the SQL for a queryset which isn't a subquery of
 another queryset, we check for existence of EMPTY_PARAM in the params
 generated for the where clause. If one is found, we know we don't need to
 execute the queryset at all.
 4. In all other cases we run the query with the empty param. The condition
 is always going to fail in the generated SQL, but that is exactly what we
 want for this ticket.

 This should also solve #26430 by the "isn't a subquery" condition in rule
 3.

--
Ticket URL: <https://code.djangoproject.com/ticket/26433#comment:1>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.fb46b9a0a34e4215767dcc9fdac50166%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to