Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15624 -- Made sure aggregations are present in SELECT

  • Loading branch information...
commit e888a9b30dd9d62a930b9244d1b5531bb17544b4 1 parent e01b5a5
Florian Hahn authored February 19, 2013 akaariai committed July 31, 2013
1  django/db/models/query.py
@@ -319,7 +319,6 @@ def aggregate(self, *args, **kwargs):
319 319
             query.add_aggregate(aggregate_expr, self.model, alias,
320 320
                                 is_summary=True)
321 321
             aggregate_names.append(alias)
322  
-        query.append_aggregate_mask(aggregate_names)
323 322
 
324 323
         return query.get_aggregation(using=self.db)
325 324
 
3  django/db/models/sql/query.py
@@ -321,6 +321,7 @@ def get_aggregation(self, using):
321 321
         # information but retrieves only the first row. Aggregate
322 322
         # over the subquery instead.
323 323
         if self.group_by is not None:
  324
+
324 325
             from django.db.models.sql.subqueries import AggregateQuery
325 326
             query = AggregateQuery(self.model)
326 327
 
@@ -996,6 +997,8 @@ def add_aggregate(self, aggregate, model, alias, is_summary):
996 997
             field_name = field_list[0]
997 998
             source = opts.get_field(field_name)
998 999
             col = field_name
  1000
+        # We want to have the alias in SELECT clause even if mask is set.
  1001
+        self.append_aggregate_mask([alias])
999 1002
 
1000 1003
         # Add the aggregate to the query
1001 1004
         aggregate.add_to_query(self, alias, col=col, source=source, is_summary=is_summary)
7  tests/aggregation_regress/tests.py
@@ -250,6 +250,13 @@ def test_aggrate_annotation(self):
250 250
             'price__max': Decimal("82.80")
251 251
         })
252 252
 
  253
+        # Regression for #15624 - Missing SELECT columns when using values, annotate
  254
+        # and aggregate in a single query
  255
+        self.assertEqual(
  256
+            Book.objects.annotate(c=Count('authors')).values('c').aggregate(Max('c')),
  257
+            {'c__max': 3}
  258
+            )
  259
+
253 260
     def test_field_error(self):
254 261
         # Bad field requests in aggregates are caught and reported
255 262
         self.assertRaises(

0 notes on commit e888a9b

Please sign in to comment.
Something went wrong with that request. Please try again.