[Django] #24753: Reverse FK conditions filter queryset when used in case statements in some cases

24 views
Skip to first unread message

Django

unread,
May 5, 2015, 8:11:15 AM5/5/15
to django-...@googlegroups.com
#24753: Reverse FK conditions filter queryset when used in case statements in some
cases
----------------------------------------------+--------------------
Reporter: mdentremont | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
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

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

Django

unread,
May 5, 2015, 8:12:33 AM5/5/15
to django-...@googlegroups.com
#24753: Reverse FK conditions filter queryset when used in case statements in some
cases
-------------------------------------+-------------------------------------

Reporter: mdentremont | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
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 mdentremont):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

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

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

Django

unread,
May 5, 2015, 8:13:55 AM5/5/15
to django-...@googlegroups.com
#24753: Reverse FK conditions filter queryset when used in case statements in some
cases
-------------------------------------+-------------------------------------

Reporter: mdentremont | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by akaariai):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/24753#comment:2>

Django

unread,
May 5, 2015, 8:34:55 AM5/5/15
to django-...@googlegroups.com
#24753: Reverse FK conditions filter queryset when used in case statements in some
cases
-------------------------------------+-------------------------------------

Reporter: mdentremont | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* severity: Normal => Release blocker
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/24753#comment:3>

Django

unread,
May 10, 2015, 7:55:24 PM5/10/15
to django-...@googlegroups.com
#24753: Reverse FK conditions filter queryset when used in case statements in some
cases
-------------------------------------+-------------------------------------

Reporter: mdentremont | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by shaib):

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

--
Ticket URL: <https://code.djangoproject.com/ticket/24753#comment:4>

Django

unread,
May 11, 2015, 11:20:47 AM5/11/15
to django-...@googlegroups.com
#24753: Reverse FK conditions filter queryset when used in case statements in some
cases
-------------------------------------+-------------------------------------
Reporter: mdentremont | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Release blocker | Resolution: duplicate

Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

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


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/24753#comment:5>

Reply all
Reply to author
Forward
0 new messages