Re: [Django] #33766: FilteredRelation resolves its conditions too late which can result in unknown alias references at SQL compilation time

18 views
Skip to first unread message

Django

unread,
Jun 9, 2022, 2:32:28 PM6/9/22
to django-...@googlegroups.com
#33766: FilteredRelation resolves its conditions too late which can result in
unknown alias references at SQL compilation time
-------------------------------------+-------------------------------------
Reporter: Daniel Schaffer | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: filteredrelation | Triage Stage: Accepted
coalesce |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Daniel Schaffer):

Thanks for that explanation Simon - I wouldn't have guessed this was quite
that complicated an issue, but that all makes sense (and an interesting
read to boot!).

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

Django

unread,
Jul 5, 2022, 6:50:01 PM7/5/22
to django-...@googlegroups.com
#33766: FilteredRelation resolves its conditions too late which can result in
unknown alias references at SQL compilation time
-------------------------------------+-------------------------------------
Reporter: Daniel Schaffer | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: filteredrelation | Triage Stage: Accepted
coalesce |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Martin Schmidt):

I have a similar issue with FilteredRelation and joins and traced it back
to a django update from 4.0.3 to 4.0.6.
My guess is that this specific issue was introduced in
https://code.djangoproject.com/ticket/33598

The provided workaround helped me a lot. Thanks!

--
Ticket URL: <https://code.djangoproject.com/ticket/33766#comment:8>

Django

unread,
Aug 17, 2022, 1:23:29 AM8/17/22
to django-...@googlegroups.com
#33766: FilteredRelation resolves its conditions too late which can result in
unknown alias references at SQL compilation time
-------------------------------------+-------------------------------------
Reporter: Daniel Schaffer | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: filteredrelation | Triage Stage: Accepted
coalesce |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

#33929 was closed as a duplicate. We should add tests for #33929 when
resolving this issue.

--
Ticket URL: <https://code.djangoproject.com/ticket/33766#comment:9>

Django

unread,
Dec 27, 2022, 12:15:31 AM12/27/22
to django-...@googlegroups.com
#33766: FilteredRelation resolves its conditions too late which can result in
unknown alias references at SQL compilation time
-------------------------------------+-------------------------------------
Reporter: Daniel Schaffer | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: filteredrelation | Triage Stage: Accepted
coalesce |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

#34229 was closed as a duplicate. We should add tests for #34229 when
resolving this issue.

--
Ticket URL: <https://code.djangoproject.com/ticket/33766#comment:10>

Django

unread,
Feb 21, 2023, 2:33:01 AM2/21/23
to django-...@googlegroups.com
#33766: FilteredRelation resolves its conditions too late which can result in
unknown alias references at SQL compilation time
-------------------------------------+-------------------------------------
Reporter: Daniel Schaffer | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: filteredrelation | Triage Stage: Accepted
coalesce |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by zhu):

The FilteredRelation cannot resolve itself too.

{{{
def test_ref_self_in_condition(self):
self.assertSequenceEqual(
Author.objects.annotate(
the_book=FilteredRelation(
"book",
condition=Q(
name=F('book__title'),
),
),
)
.filter(
the_book__isnull=False,
),
[],
)

}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33766#comment:11>

Django

unread,
Apr 16, 2023, 4:53:31 AM4/16/23
to django-...@googlegroups.com
#33766: FilteredRelation resolves its conditions too late which can result in
unknown alias references at SQL compilation time
-------------------------------------+-------------------------------------
Reporter: Daniel Schaffer | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: filteredrelation | Triage Stage: Accepted
coalesce |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* cc: Sarah Boyce (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/33766#comment:12>

Django

unread,
Apr 19, 2023, 4:33:17 PM4/19/23
to django-...@googlegroups.com
#33766: FilteredRelation resolves its conditions too late which can result in
unknown alias references at SQL compilation time
-------------------------------------+-------------------------------------
Reporter: Daniel Schaffer | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: filteredrelation | Triage Stage: Accepted
coalesce |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Francesco Panico):

Could this bug be resolved after fixing [[ticket:34362]]? In the PR I
encountered exactly this error

--
Ticket URL: <https://code.djangoproject.com/ticket/33766#comment:13>

Django

unread,
Apr 19, 2023, 4:33:44 PM4/19/23
to django-...@googlegroups.com
#33766: FilteredRelation resolves its conditions too late which can result in
unknown alias references at SQL compilation time
-------------------------------------+-------------------------------------
Reporter: Daniel Schaffer | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: filteredrelation | Triage Stage: Accepted
coalesce |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Francesco Panico):

* cc: Francesco Panico (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/33766#comment:14>

Django

unread,
Apr 21, 2023, 2:43:55 AM4/21/23
to django-...@googlegroups.com
#33766: FilteredRelation resolves its conditions too late which can result in
unknown alias references at SQL compilation time
-------------------------------------+-------------------------------------
Reporter: Daniel Schaffer | Owner: Simon
| Charette
Type: Bug | Status: assigned

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: filteredrelation | Triage Stage: Accepted
coalesce |
Has patch: 1 | Needs documentation: 0

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

* owner: nobody => Simon Charette
* status: new => assigned
* has_patch: 0 => 1


Comment:

Francesco, it's the other way around, fixing this bug is required to
address #34362.

--
Ticket URL: <https://code.djangoproject.com/ticket/33766#comment:15>

Django

unread,
Apr 24, 2023, 2:33:30 AM4/24/23
to django-...@googlegroups.com
#33766: FilteredRelation resolves its conditions too late which can result in
unknown alias references at SQL compilation time
-------------------------------------+-------------------------------------
Reporter: Daniel Schaffer | Owner: Simon
| Charette
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: filteredrelation | Triage Stage: Ready for
coalesce | checkin

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

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/33766#comment:16>

Django

unread,
Apr 24, 2023, 3:36:59 AM4/24/23
to django-...@googlegroups.com
#33766: FilteredRelation resolves its conditions too late which can result in
unknown alias references at SQL compilation time
-------------------------------------+-------------------------------------
Reporter: Daniel Schaffer | Owner: Simon
| Charette
Type: Bug | Status: closed

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: filteredrelation | Triage Stage: Ready for
coalesce | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

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


Comment:

In [changeset:"d660cee5bc68b597503c2a16f3d9928d52f93fb4" d660cee5]:
{{{
#!CommitTicketReference repository=""
revision="d660cee5bc68b597503c2a16f3d9928d52f93fb4"
Fixed #33766 -- Resolved FilteredRelation.condition at referencing time.

The previous implementation resolved condition at Join compilation time
which required introducing a specialized expression resolving mode to
alter the join reuse logic solely during that phase.

FilteredRelation.condition is now resolved when the relation is first
referenced which maintains the existing behavior while allowing the
removal of the specialized resolving mode and address an issue where
conditions couldn't spawn new joins.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33766#comment:17>

Django

unread,
Apr 24, 2023, 3:37:00 AM4/24/23
to django-...@googlegroups.com
#33766: FilteredRelation resolves its conditions too late which can result in
unknown alias references at SQL compilation time
-------------------------------------+-------------------------------------
Reporter: Daniel Schaffer | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: filteredrelation | Triage Stage: Ready for
coalesce | 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:"83c9765f45e4622e4a5af3adcd92263a28b13624" 83c9765]:
{{{
#!CommitTicketReference repository=""
revision="83c9765f45e4622e4a5af3adcd92263a28b13624"
Refs #33766 -- Removed sql.Query.build_filtered_relation_q().

It was a copy of sql.Query._add_q that avoided JOIN updates.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33766#comment:19>

Django

unread,
Apr 24, 2023, 3:37:00 AM4/24/23
to django-...@googlegroups.com
#33766: FilteredRelation resolves its conditions too late which can result in
unknown alias references at SQL compilation time
-------------------------------------+-------------------------------------
Reporter: Daniel Schaffer | Owner: Simon
| Charette
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: filteredrelation | Triage Stage: Ready for
coalesce | 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:"1a13161eab2d040990734f80b3eedea354f6a4ba" 1a13161]:
{{{
#!CommitTicketReference repository=""
revision="1a13161eab2d040990734f80b3eedea354f6a4ba"
Refs #33766 -- Removed unused Join.equals().

It's unused now that the specialized FilteredRelation.as_sql logic is
no more.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33766#comment:18>

Reply all
Reply to author
Forward
0 new messages