diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
a
|
b
|
|
473 | 473 | group_by = self.query.group_by or [] |
474 | 474 | |
475 | 475 | extra_selects = [] |
476 | | for extra_select, extra_params in self.query.extra_select.itervalues(): |
477 | | extra_selects.append(extra_select) |
478 | | params.extend(extra_params) |
| 476 | for extra_select_key in self.query.extra_select.iterkeys(): |
| 477 | extra_selects.append(extra_select_key) |
479 | 478 | for col in group_by + self.query.related_select_cols + extra_selects: |
480 | 479 | if isinstance(col, (list, tuple)): |
481 | 480 | result.append('%s.%s' % (qn(col[0]), qn(col[1]))) |
diff --git a/tests/regressiontests/aggregation_regress/tests.py b/tests/regressiontests/aggregation_regress/tests.py
new file mode 100644
-
|
+
|
|
| 1 | from django.test import TestCase |
| 2 | from django.db.models import Count |
| 3 | |
| 4 | from regressiontests.aggregation_regress.models import * |
| 5 | |
| 6 | |
| 7 | class AggregationTests(TestCase): |
| 8 | |
| 9 | def test_annotate_with_extra(self): |
| 10 | """ |
| 11 | Regression test for #11916: Extra params + aggregation creates |
| 12 | incorrect SQL. |
| 13 | """ |
| 14 | shortest_book_sql = """ |
| 15 | SELECT name |
| 16 | FROM aggregation_regress_book b |
| 17 | WHERE b.publisher_id = aggregation_regress_publisher.id |
| 18 | ORDER BY b.pages |
| 19 | LIMIT 1 |
| 20 | """ |
| 21 | # tests that this query does not raise a DatabaseError due to the full |
| 22 | # subselect being (erroneously) added to the GROUP BY parameters |
| 23 | qs = Publisher.objects.extra(select={ |
| 24 | 'name_of_shortest_book': shortest_book_sql, |
| 25 | }).annotate(total_books=Count('book')) |
| 26 | # force execution of the query |
| 27 | list(qs) |