1
0
mirror of https://github.com/django/django.git synced 2025-01-30 20:19:22 +00:00

[5.2.x] Fixed #36109 -- Fixed RecursionError when stacking FilteredRelation joins.

Backport of 8eca4077f60fa0705ecfd9437c9ceaeef7a3808b from main.
This commit is contained in:
Peter DeVita 2025-01-17 13:22:24 -05:00 committed by Sarah Boyce
parent b285cc787e
commit 720ef7a867
3 changed files with 25 additions and 0 deletions

View File

@ -823,6 +823,7 @@ answer newbie questions, and generally made Django that much better:
Petar Marić <http://www.petarmaric.com/>
Pete Crosier <pete.crosier@gmail.com>
peter@mymart.com
Peter DeVita <pmdevita2643@gmail.com>
Peter Sheats <sheats@gmail.com>
Peter van Kampen
Peter Zsoldos <http://zsoldosp.eu>

View File

@ -1946,6 +1946,8 @@ class Query(BaseExpression):
reuse = can_reuse if join.m2m else None
alias = self.join(connection, reuse=reuse)
joins.append(alias)
if join.filtered_relation and can_reuse is not None:
can_reuse.add(alias)
return JoinInfo(final_field, targets, opts, joins, path, final_transformer)
def trim_joins(self, targets, joins, path):

View File

@ -582,6 +582,28 @@ class FilteredRelationTests(TestCase):
lambda x: (x.author, x.book_title, x.preferred_by_author_pk),
)
def test_three_level_nested_chained_relations(self):
borrower = Borrower.objects.create(name="Jenny")
Reservation.objects.create(
borrower=borrower,
book=self.book1,
state=Reservation.STOPPED,
)
qs = Author.objects.annotate(
my_books=FilteredRelation("book"),
my_reserved_books=FilteredRelation(
"my_books__reservation",
condition=Q(my_books__reservation__state=Reservation.STOPPED),
),
my_readers=FilteredRelation(
"my_reserved_books__borrower",
condition=Q(my_reserved_books__borrower=borrower),
),
)
self.assertSequenceEqual(
qs.filter(my_readers=borrower).values_list("name", flat=True), ["Alice"]
)
def test_deep_nested_foreign_key(self):
qs = (
Book.objects.annotate(