From 385ae343fbd2355a09ba0f459445bf04406db60b Mon Sep 17 00:00:00 2001 From: Luke Plant Date: Sat, 7 May 2011 19:02:51 +0000 Subject: [PATCH] Fixed #15709 - Duplicated group_by condition Thanks to ziangsong for report, and to mk for the patch git-svn-id: http://code.djangoproject.com/svn/django/trunk@16180 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/sql/compiler.py | 4 ++++ tests/regressiontests/aggregation_regress/tests.py | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index ace9cf4c88..841ec12f2d 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -493,7 +493,11 @@ class SQLCompiler(object): params.extend(extra_params) cols = (group_by + self.query.select + self.query.related_select_cols + extra_selects) + seen = set() for col in cols: + if col in seen: + continue + seen.add(col) if isinstance(col, (list, tuple)): result.append('%s.%s' % (qn(col[0]), qn(col[1]))) elif hasattr(col, 'as_sql'): diff --git a/tests/regressiontests/aggregation_regress/tests.py b/tests/regressiontests/aggregation_regress/tests.py index 0bb68991ea..14104210af 100644 --- a/tests/regressiontests/aggregation_regress/tests.py +++ b/tests/regressiontests/aggregation_regress/tests.py @@ -462,6 +462,12 @@ class AggregationTests(TestCase): lambda b: b.name ) + # Regression for #15709 - Ensure each group_by field only exists once + # per query + qs = Book.objects.values('publisher').annotate(max_pages=Max('pages')).order_by() + grouping, gb_params = qs.query.get_compiler(qs.db).get_grouping() + self.assertEqual(len(grouping), 1) + def test_duplicate_alias(self): # Regression for #11256 - duplicating a default alias raises ValueError. self.assertRaises(ValueError, Book.objects.all().annotate, Avg('authors__age'), authors__age__avg=Avg('authors__age'))