#21956: Inconsistent behaviour of Q objects
----------------------------------------------+--------------------
     Reporter:  debanshuk                     |      Owner:  nobody
         Type:  Bug                           |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.5
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  0
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 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.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/21956>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.d0b9f75d6e02e00f87033dc8de2407ee%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to