Skip to content

Commit

Permalink
Fixed #34450 -- Fixed multi-valued JOIN reuse when filtering by expre…
Browse files Browse the repository at this point in the history
…ssions.

Thanks Roman Odaisky for the report.
  • Loading branch information
charettes authored and felixxm committed Apr 4, 2023
1 parent 79a3ea8 commit 0e1aae7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion django/db/models/sql/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ def build_filter(
if not getattr(filter_expr, "conditional", False):
raise TypeError("Cannot filter against a non-conditional expression.")
condition = filter_expr.resolve_expression(
self, allow_joins=allow_joins, summarize=summarize
self, allow_joins=allow_joins, reuse=can_reuse, summarize=summarize
)
if not isinstance(condition, Lookup):
condition = self.build_lookup(["exact"], condition, True)
Expand Down
19 changes: 19 additions & 0 deletions tests/lookup/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,9 @@ def setUpTestData(cls):
cls.s1 = Season.objects.create(year=1942, gt=1942)
cls.s2 = Season.objects.create(year=1842, gt=1942, nulled_text_field="text")
cls.s3 = Season.objects.create(year=2042, gt=1942)
Game.objects.create(season=cls.s1, home="NY", away="Boston")
Game.objects.create(season=cls.s1, home="NY", away="Tampa")
Game.objects.create(season=cls.s3, home="Boston", away="Tampa")

def test_annotate(self):
qs = Season.objects.annotate(equal=Exact(F("year"), 1942))
Expand Down Expand Up @@ -1527,3 +1530,19 @@ def test_conditional_expression(self):
{"year": 2042, "century": "other"},
],
)

def test_multivalued_join_reuse(self):
self.assertEqual(
Season.objects.get(Exact(F("games__home"), "NY"), games__away="Boston"),
self.s1,
)
self.assertEqual(
Season.objects.get(Exact(F("games__home"), "NY") & Q(games__away="Boston")),
self.s1,
)
self.assertEqual(
Season.objects.get(
Exact(F("games__home"), "NY") & Exact(F("games__away"), "Boston")
),
self.s1,
)

0 comments on commit 0e1aae7

Please sign in to comment.