From d660cee5bc68b597503c2a16f3d9928d52f93fb4 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Thu, 20 Apr 2023 00:25:57 -0400 Subject: 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. --- tests/filtered_relation/tests.py | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'tests') diff --git a/tests/filtered_relation/tests.py b/tests/filtered_relation/tests.py index 0fce8b092a..2cceeb82e7 100644 --- a/tests/filtered_relation/tests.py +++ b/tests/filtered_relation/tests.py @@ -793,6 +793,47 @@ class FilteredRelationAggregationTests(TestCase): qs.annotate(total=Count("pk")).values("total"), [{"total": 1}] ) + def test_condition_spans_join(self): + self.assertSequenceEqual( + Book.objects.annotate( + contains_editor_author=FilteredRelation( + "author", condition=Q(author__name__icontains=F("editor__name")) + ) + ).filter( + contains_editor_author__isnull=False, + ), + [self.book1], + ) + + def test_condition_spans_join_chained(self): + self.assertSequenceEqual( + Book.objects.annotate( + contains_editor_author=FilteredRelation( + "author", condition=Q(author__name__icontains=F("editor__name")) + ), + contains_editor_author_ref=FilteredRelation( + "author", + condition=Q(author__name=F("contains_editor_author__name")), + ), + ).filter( + contains_editor_author_ref__isnull=False, + ), + [self.book1], + ) + + def test_condition_self_ref(self): + self.assertSequenceEqual( + Book.objects.annotate( + contains_author=FilteredRelation( + "author", + condition=Q(title__icontains=F("author__name")), + ) + ).filter( + contains_author__isnull=False, + ), + [self.book1], + ) + class FilteredRelationAnalyticalAggregationTests(TestCase): @classmethod -- cgit v1.3