#24279: ORing with an empty Q() produces inconsistent results
----------------------------------------------+---------------------------
Reporter: ris | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.7
Severity: Normal | Keywords: Q empty in or
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+---------------------------
Using django 1.7.4, postgres 9.1.
Minimal testcase models.py:
{{{
from django.db import models
class ModelA ( models.Model ):
pass
}}}
then:
{{{
>>> for i in xrange ( 1 , 5 ):
... ModelA.objects.create ( pk = i )
>>> ModelA.objects.filter ( Q ( pk__in = () ) )
[]
}}}
correctly returning no results.
also
{{{
>>> ModelA.objects.filter ( Q ( pk__in = (1,2,) ) | Q () )
[<ModelA: 1>, <ModelA: 2>]
}}}
again, correct
however
{{{
>>> ModelA.objects.filter ( Q ( pk__in = () ) | Q () )
[<ModelA: 1>, <ModelA: 2>, <ModelA: 3>, <ModelA: 4>]
}}}
where I would have expected no instances to be returned.
The empty Q here has the effect of turning the {{{pk__in}}} into a no-op.
Interestingly, reformulating it as a (no-result-returning) subquery
produces the expected result:
{{{
>>> ModelA.objects.filter ( Q ( pk__in = ModelA.objects.filter (
pk__isnull = True ) ) | Q () )
[]
}}}
I realize that OR'ing with an empty Q may not be something you're really
supposed to do, but sometimes it does happen and this behaviour is quite
unexpected.
Apologies if this is a duplicate but I was unable to find an existing
ticket covering this.
--
Ticket URL: <https://code.djangoproject.com/ticket/24279>
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/046.33e34609d6bd6f96804972d53d6b86dc%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.