﻿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
21956	Inconsistent behaviour of Q objects	Debanshu Kundu	nobody	"Suppose we have a contact model with following definition:

{{{
class Contact(models.Model):
    name = models.CharField()
    company_uid = models.IntegerField(null=True, blank=True)
}}}

Consider following two queries:

    `Contact.objects.filter(~(Q(name='ABC') | Q(company_uid=5)))`
    `Contact.objects.filter(~Q(name='ABC') & ~Q(company_uid=5))`

These queries are exactly the same, right? Just used DeMorgan's law i.e ""not (A or B)"" is the same as ""(not A) and (not B)"". But the generated SQL of these queries says something else:

    `SELECT ""profiles_contact"".""id"", ""profiles_contact"".""name"", ""profiles_contact"".""company_uid"" FROM ""profiles_contact"" WHERE NOT ((""profiles_contact"".""name"" = ABC  OR ""profiles_contact"".""company_uid"" = 5 ))`

    `SELECT ""profiles_contact"".""id"", ""profiles_contact"".""name"", ""profiles_contact"".""company_uid"" FROM ""profiles_contact"" WHERE (NOT (""profiles_contact"".""name"" = ABC ) AND NOT ((""profiles_contact"".""company_uid"" = 5  AND ""profiles_contact"".""company_uid"" IS NOT NULL)))`


(The desired query is the one with `IS NOT NULL` condition)


This issue only exists till version 1.5, it has been fixed in Django-1.6. There is already a closed bug (https://code.djangoproject.com/ticket/21192) which was similar to this one. The bug was closed stating that the issue has been fixed in Django-1.6 and changes required to back-patch the fixes to 1.5 are just too big. But I believe this issue is more severe than the issue stated the that bug, as it is a more general case and should be back-patched to 1.5 and older version too."	Bug	closed	Database layer (models, ORM)	1.5	Normal	wontfix			Unreviewed	0	0	0	0	0	0
