﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24279	ORing with an empty Q() produces inconsistent results	ris	nobody	"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."	Bug	closed	Database layer (models, ORM)	1.7	Normal	fixed	Q empty in or	Дилян Палаузов	Accepted	0	0	0	0	0	0
