Ticket #11916: ticket_11916.diff

File ticket_11916.diff, 1.9 KB (added by Tobias McNulty, 15 years ago)

patch with test

  • django/db/models/sql/compiler.py

    diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
    a b  
    473473            group_by = self.query.group_by or []
    474474
    475475            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)
    479478            for col in group_by + self.query.related_select_cols + extra_selects:
    480479                if isinstance(col, (list, tuple)):
    481480                    result.append('%s.%s' % (qn(col[0]), qn(col[1])))
  • new file tests/regressiontests/aggregation_regress/tests.py

    diff --git a/tests/regressiontests/aggregation_regress/tests.py b/tests/regressiontests/aggregation_regress/tests.py
    new file mode 100644
    - +  
     1from django.test import TestCase
     2from django.db.models import Count
     3
     4from regressiontests.aggregation_regress.models import *
     5
     6
     7class 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)
Back to Top