Opened 33 minutes ago
Last modified 26 minutes ago
#37372 assigned Bug
Handling of null values in __in lookups is inconsistent
| Reported by: | Annabelle Wiegart | Owned by: | Annabelle Wiegart |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 6.1 |
| Severity: | Normal | Keywords: | null, in, lookup |
| Cc: | Simon Charette | Triage Stage: | Accepted |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
When the right-hand side of an __in lookup contains None, several problems are surfaced.
- The resulting querysets differ from the same query being constructed with
Qqueries combined with OR. .filter()and.exclude()produce non-complementary querysets.- Objects with null None values are discarded.
This happens when testing against a single table as well when spanning relationships.
The problems are related to the interference between two tickets:
#20024 was created 14 years ago. The suggested change made sense back then, but conflicts with the fix for #31667 implemented in the meantime. This led to the fix for #20024 being ineffective, and the targeting of null values in __in lookups being made impossible.
A bit more context for 2.: It was decided to discard None from the rhs for __in lookups with .filter(), since NULL is never equal to anything. That means that the None values have to be included in the .exclude() queryset in order for .filter() and .exclude() to produce complementary querysets.
DryORM fiddle: https://dryorm.xterm.info/in-lookups-with-none
Change History (1)
comment:1 by , 26 minutes ago
| Cc: | added |
|---|---|
| Component: | Uncategorized → Database layer (models, ORM) |
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Bug |
Annabelle and I worked on this project during the Django on the Med 2027 sprint.