Commits

Anonymous committed 8b4a1e3

Refs #10113 -- Modified the generated SQL to remove redundant GROUP BY elements, and modified the test added in [9788] to remove a test result that depended on a potentially ambiguous database ordering.

  • Participants
  • Parent commits 90de3f2

Comments (0)

Files changed (2)

File django/db/models/sql/query.py

                 # 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))

File tests/regressiontests/aggregation_regress/models.py

 
 # 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: The Definitive Guide to Django: Web Development Done Right>, <Book: Practical Django Projects>, <Book: Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp>, <Book: Python Web Development with Django>, <Book: Artificial Intelligence: A Modern Approach>, <Book: Sams Teach Yourself Django in 24 Hours>]
+>>> Book.objects.annotate(num_authors=Count('authors')).order_by('publisher__name', 'name')
+[<Book: Practical Django Projects>, <Book: The Definitive Guide to Django: Web Development Done Right>, <Book: Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp>, <Book: Artificial Intelligence: A Modern Approach>, <Book: Python Web Development with Django>, <Book: Sams Teach Yourself Django in 24 Hours>]
 
 """
 }