Opened 3 years ago
Closed 3 years ago
#32896 closed Bug (duplicate)
Exclude on expressions not being negated as expected
Reported by: | Alex Henman | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 3.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Quite similar to #23797 which looks like it was fixed in 3.2.
Consider the same model definition:
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:
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.
Note:
See TracTickets
for help on using tickets.
Duplicate of #32398.