[Django] #33598: Using multiple FilteredRelation with different filters but for same relation is ignored.

60 views
Skip to first unread message

Django

unread,
Mar 23, 2022, 4:32:12 PM3/23/22
to django-...@googlegroups.com
#33598: Using multiple FilteredRelation with different filters but for same
relation is ignored.
-------------------------------------+-------------------------------------
Reporter: lind- | Owner: nobody
marcus |
Type: Bug | Status: new
Component: Database | Version: 4.0
layer (models, ORM) |
Severity: Normal | Keywords: FilteredRelation
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I have a relation that ALWAYS have at least 1 entry with `is_all=True` and
then I have an optional entry that could have `is_all=False` but instead
have `zone` set.

I'm trying to use `FilteredRelation` together with `Case(When())` to
ensure that it use the zone level one (if exist) and fall back on "All" if
zone do not exist.


{{{
from django.db.models import FilteredRelation

qs.alias(
relation_zone=FilteredRelation(
"myrelation__nested",
condition=Q(myrelation__nested__zone=F("zone"))
),
relation_all=FilteredRelation(
"myrelation__nested",
condition=Q(myrelation__nested__is_all=True)
),
price_zone=F("relation_zone__price")
).annotate(
price_final=Case(
When(
price_zone__isnull=True,
then=F("relation_all__price"),
),
default=F("price_zone")
)
)
}}}

I noticed that when using multiple `FilteredRelation` with the same
relation (`myrelation__nested`) it actually just generates a single SQL
statement and ignores the other. So in this case if I do
`print(str(qs.query))` I would only see a join for `relation_zone`. Not
for `relation_all`.

Is this intended behavior or should I be able to do the thing above?

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

Django

unread,
Mar 23, 2022, 7:23:46 PM3/23/22
to django-...@googlegroups.com
#33598: Using multiple FilteredRelation with different filters but for same
relation is ignored.
-------------------------------------+-------------------------------------
Reporter: lind-marcus | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: FilteredRelation | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by lind-marcus:

Old description:

New description:


{{{
from django.db.models import FilteredRelation

JOIN (if inspecting the raw SQL) and ignores the other. So in this case if


I do `print(str(qs.query))` I would only see a join for `relation_zone`.
Not for `relation_all`.

Is this intended behavior or should I be able to do the thing above?

--

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

Django

unread,
Mar 24, 2022, 2:55:48 AM3/24/22
to django-...@googlegroups.com
#33598: Using multiple FilteredRelation with different filters but for same
relation is ignored.
-------------------------------------+-------------------------------------
Reporter: lind-marcus | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: FilteredRelation | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* cc: Nick Pope (added)
* severity: Normal => Release blocker
* stage: Unreviewed => Accepted


Comment:

Thanks for the report!

Regression in 0c71e0f9cfa714a22297ad31dd5613ee548db379
Reproduced at 1cf60ce6017d904024ee132f7edae0b4b821a954.

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

Django

unread,
Mar 28, 2022, 7:28:25 AM3/28/22
to django-...@googlegroups.com
#33598: Using multiple FilteredRelation with different filters but for same
relation is ignored.
-------------------------------------+-------------------------------------
Reporter: lind-marcus | Owner: Mariusz
| Felisiak
Type: Bug | Status: assigned

Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: FilteredRelation | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: nobody => Mariusz Felisiak
* status: new => assigned


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

Django

unread,
Mar 29, 2022, 6:13:17 AM3/29/22
to django-...@googlegroups.com
#33598: Using multiple FilteredRelation with different filters but for same
relation is ignored.
-------------------------------------+-------------------------------------
Reporter: lind-marcus | Owner: Mariusz
| Felisiak
Type: Bug | Status: assigned
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: FilteredRelation | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/15554 PR]

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

Django

unread,
Mar 29, 2022, 10:21:50 AM3/29/22
to django-...@googlegroups.com
#33598: Using multiple FilteredRelation with different filters but for same
relation is ignored.
-------------------------------------+-------------------------------------
Reporter: lind-marcus | Owner: Mariusz
| Felisiak
Type: Bug | Status: assigned
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: FilteredRelation | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* stage: Accepted => Ready for checkin


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

Django

unread,
Mar 30, 2022, 1:32:48 AM3/30/22
to django-...@googlegroups.com
#33598: Using multiple FilteredRelation with different filters but for same
relation is ignored.
-------------------------------------+-------------------------------------
Reporter: lind-marcus | Owner: Mariusz
| Felisiak
Type: Bug | Status: closed

Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Release blocker | Resolution: fixed

Keywords: FilteredRelation | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"fac662f4798f7e4e0ed9be6b4fb4a87a80810a68" fac662f]:
{{{
#!CommitTicketReference repository=""
revision="fac662f4798f7e4e0ed9be6b4fb4a87a80810a68"
Fixed #33598 -- Reverted "Removed unnecessary reuse_with_filtered_relation
argument from Query methods."

Thanks lind-marcus for the report.

This reverts commit 0c71e0f9cfa714a22297ad31dd5613ee548db379.

Regression in 0c71e0f9cfa714a22297ad31dd5613ee548db379.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33598#comment:6>

Django

unread,
Mar 30, 2022, 1:33:28 AM3/30/22
to django-...@googlegroups.com
#33598: Using multiple FilteredRelation with different filters but for same
relation is ignored.
-------------------------------------+-------------------------------------
Reporter: lind-marcus | Owner: Mariusz
| Felisiak
Type: Bug | Status: closed
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: FilteredRelation | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"7d540d67a8fb17f820e1657ccb1509af86b27582" 7d540d67]:
{{{
#!CommitTicketReference repository=""
revision="7d540d67a8fb17f820e1657ccb1509af86b27582"
[4.0.x] Fixed #33598 -- Reverted "Removed unnecessary


reuse_with_filtered_relation argument from Query methods."

Thanks lind-marcus for the report.

This reverts commit 0c71e0f9cfa714a22297ad31dd5613ee548db379.

Regression in 0c71e0f9cfa714a22297ad31dd5613ee548db379.
Backport of fac662f4798f7e4e0ed9be6b4fb4a87a80810a68 from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33598#comment:7>

Reply all
Reply to author
Forward
0 new messages