diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 6170cc48a45..88847d87e1b 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -385,7 +385,8 @@ class BaseQuery(object): # other than MySQL), then any fields mentioned in the # ordering clause needs to be in the group by clause. if not self.connection.features.allows_group_by_pk: - grouping.extend(ordering_group_by) + grouping.extend([col for col in ordering_group_by + if col not in grouping]) else: ordering = self.connection.ops.force_no_ordering() result.append('GROUP BY %s' % ', '.join(grouping)) diff --git a/tests/regressiontests/aggregation_regress/models.py b/tests/regressiontests/aggregation_regress/models.py index f41d9a252be..a87ce1cd1d8 100644 --- a/tests/regressiontests/aggregation_regress/models.py +++ b/tests/regressiontests/aggregation_regress/models.py @@ -176,8 +176,8 @@ FieldError: Cannot resolve keyword 'foo' into field. Choices are: authors, id, i # Regression for #10113 - Fields mentioned in order_by() must be included in the GROUP BY. # This only becomes a problem when the order_by introduces a new join. ->>> Book.objects.annotate(num_authors=Count('authors')).order_by('publisher__name') -[, , , , , ] +>>> Book.objects.annotate(num_authors=Count('authors')).order_by('publisher__name', 'name') +[, , , , , ] """ }