[Django] #32896: Exclude on expressions not being negated as expected

0 views
Skip to first unread message

Django

unread,
Jul 2, 2021, 5:40:23 AM7/2/21
to django-...@googlegroups.com
#32896: Exclude on expressions not being negated as expected
-------------------------------------+-------------------------------------
Reporter: Alex | Owner: nobody
Henman |
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Quite similar to #23797 which looks like it was fixed in 3.2.

Consider the same model definition:

{{{
#!python

from django.db import models

class Rectangle(models.Model):
length = models.IntegerField(null=True)
width = models.IntegerField(null=True)
}}}

Then compare the behaviour of excluding when directly accessing the field
vs. using an alias or annotation:

{{{
#!python

In [5]: str(Rectangle.objects.exclude(length=123).values("pk").query)
Out[5]: 'SELECT "geometry_rectangle"."id" FROM "geometry_rectangle" WHERE
NOT ("geometry_rectangle"."length" = 123 AND "geometry_rectangle"."length"
IS NOT NULL)'

In [6]:
str(Rectangle.objects.alias(aliased_length=F("length")).exclude(aliased_length=123).values("pk").query)
Out[6]: 'SELECT "geometry_rectangle"."id" FROM "geometry_rectangle" WHERE
NOT ("geometry_rectangle"."length" = 123)'

In [7]:
str(Rectangle.objects.annotate(aliased_length=F("length")).exclude(aliased_length=123).values("pk").query)
Out[7]: 'SELECT "geometry_rectangle"."id" FROM "geometry_rectangle" WHERE
NOT ("geometry_rectangle"."length" = 123)'
}}}

The expected behaviour would be that when using an alias or annotation,
the {{{IS NOT NULL}}} condition would be added when using exclude.

--
Ticket URL: <https://code.djangoproject.com/ticket/32896>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 2, 2021, 6:05:14 AM7/2/21
to django-...@googlegroups.com
#32896: Exclude on expressions not being negated as expected
-------------------------------------+-------------------------------------
Reporter: Alex Henman | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #32398.

--
Ticket URL: <https://code.djangoproject.com/ticket/32896#comment:1>

Reply all
Reply to author
Forward
0 new messages