Opened 9 years ago

Closed 9 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 Matt d'Entremont, 9 years ago

I've added a unit test which demonstrates the issue here: https://github.com/django/django/pull/4613

comment:2 by Anssi Kääriäinen, 9 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 Tim Graham, 9 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

comment:4 by Shai Berger, 9 years ago

See also #24766, similar problems with non-aggregating annotations.

comment:5 by Tim Graham, 9 years ago

Resolution: duplicate
Status: newclosed

The proposed test is similar to the one added with the fix for #24766 and now passes.

Note: See TracTickets for help on using tickets.
Back to Top