Skip to content

Commit

Permalink
Fixed #15709 - Duplicated group_by condition
Browse files Browse the repository at this point in the history
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
  • Loading branch information
spookylukey committed May 7, 2011
1 parent 32bd953 commit 385ae34
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions django/db/models/sql/compiler.py
Expand Up @@ -493,7 +493,11 @@ def get_grouping(self):
params.extend(extra_params) params.extend(extra_params)
cols = (group_by + self.query.select + cols = (group_by + self.query.select +
self.query.related_select_cols + extra_selects) self.query.related_select_cols + extra_selects)
seen = set()
for col in cols: for col in cols:
if col in seen:
continue
seen.add(col)
if isinstance(col, (list, tuple)): if isinstance(col, (list, tuple)):
result.append('%s.%s' % (qn(col[0]), qn(col[1]))) result.append('%s.%s' % (qn(col[0]), qn(col[1])))
elif hasattr(col, 'as_sql'): elif hasattr(col, 'as_sql'):
Expand Down
6 changes: 6 additions & 0 deletions tests/regressiontests/aggregation_regress/tests.py
Expand Up @@ -462,6 +462,12 @@ def test_more_more(self):
lambda b: b.name 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): def test_duplicate_alias(self):
# Regression for #11256 - duplicating a default alias raises ValueError. # 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')) self.assertRaises(ValueError, Book.objects.all().annotate, Avg('authors__age'), authors__age__avg=Avg('authors__age'))
Expand Down

0 comments on commit 385ae34

Please sign in to comment.