Opened 10 years ago
Closed 10 years ago
#24753 closed Bug (duplicate)
Reverse FK conditions filter queryset when used in case statements in some cases
Reported by: | Matt d'Entremont | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.8 |
Severity: | Release blocker | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I'm running into some odd behaviour with the new conditional support added in 1.8.
My use case is the following:
- Annotate a queryset with the count of reverse FKs, matching a condition
This works fine given a many-to-one relationship, however not with many-to-one/none.
With the many-to-one/none, it appears an INNER JOIN is made, which removes objects from my queryset which an empty reverse FK set.
To summarize:
Expected:
Annotate all objects with the a count of reverse FKs matching a condition
Actual:
Annotate only objects objects with 1 or more associated reverse FKs, with a count of the reverse FKs matching a condition
Change History (5)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
I have a guess what could be happening here. The ORM generates joins as INNER JOINs when using build_filter(). We should manually promote those joins to OUTER JOINs afterwards. Can't work on this today, so I'll check this tomorrow.
comment:3 by , 10 years ago
Severity: | Normal → Release blocker |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:5 by , 10 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
The proposed test is similar to the one added with the fix for #24766 and now passes.
I've added a unit test which demonstrates the issue here: https://github.com/django/django/pull/4613