#24196: Filtering __in a sliced queryset with a 0 limit raises an unexpected
error
----------------------------------------------+--------------------
Reporter: BertrandBordage | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.7
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
{{{AnyModel.objects.filter(pk__in=AnyModel.objects.all()[i:i])}}} (where
{{{i}}} is a positive integer) raises a {{{ProgrammingError}}} at least on
Django 1.6 and 1.7. It most certainly affects all other Django versions
too.
That’s because the ORM avoids converting {{{AnyModel.objects.all()[:0]}}}
to a SQL query, while {{{.filter(in___=…)}}} tries to include that
inexistant SQL query. The resulting SQL looks like {{{SELECT * FROM
any_model WHERE id IN ()}}}, which is invalid.
A working fix is to change
[https://github.com/django/django/blob/1.7.3/django/db/models/sql/compiler.py#L91
line 91 of compiler.py] to {{{raise EmptyResultSet}}} instead of {{{return
'', ()}}}.
{{{WhereNode.as_sql}}} then
[https://github.com/django/django/blob/1.7.3/django/db/models/sql/where.py#L104-111
catches this exception] and notes that it should select nothing. However,
I’m not sure that it doesn’t break something else. I can make a pull
request with a unit test if you want.
In the meantime, a workaround is to detect whether the inner QuerySet
verifies {{{queryset.query.low_mark == queryset.query.high_mark}}}, and do
something relevant if it does.
--
Ticket URL: <https://code.djangoproject.com/ticket/24196>
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/058.efaa94d284d13d1fff162cd2b8c07f48%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.