From 1c4f5f314e2b0c77b3fa0c75f703218e7e06f4be Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Wed, 14 Jun 2023 00:28:42 -0400 Subject: Refs #32143 -- Removed superflous constraints on excluded query. The outer query reference is not necessary when alias can be reused and can even be harmful by confusing query planers. Refs #34597. --- tests/queries/tests.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/queries/tests.py b/tests/queries/tests.py index 9578777b11..43cfd9b7c0 100644 --- a/tests/queries/tests.py +++ b/tests/queries/tests.py @@ -3231,7 +3231,7 @@ class ExcludeTests(TestCase): [self.r2], ) - def test_ticket14511(self): + def test_exclude_m2m_through(self): alex = Person.objects.get_or_create(name="Alex")[0] jane = Person.objects.get_or_create(name="Jane")[0] @@ -3267,7 +3267,16 @@ class ExcludeTests(TestCase): .distinct() .order_by("name") ) - self.assertSequenceEqual(alex_nontech_employers, [google, intel, microsoft]) + with self.assertNumQueries(1) as ctx: + self.assertSequenceEqual(alex_nontech_employers, [google, intel, microsoft]) + sql = ctx.captured_queries[0]["sql"] + # Company's ID should appear in SELECT and INNER JOIN, not in EXISTS as + # the outer query reference is not necessary when an alias is reused. + company_id = "%s.%s" % ( + connection.ops.quote_name(Company._meta.db_table), + connection.ops.quote_name(Company._meta.get_field("id").column), + ) + self.assertEqual(sql.count(company_id), 2) def test_exclude_reverse_fk_field_ref(self): tag = Tag.objects.create() -- cgit v1.3