Ticket #11916: ticket_11916.3.diff

File ticket_11916.3.diff, 2.1 KB (added by paluh, 14 years ago)

patch + unit test

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

     
    484484                elif hasattr(col, 'as_sql'):
    485485                    result.append(col.as_sql(qn))
    486486                else:
    487                     result.append(str(col))
     487                    result.append('(%s)' % str(col))
    488488        return result, params
    489489
    490490    def fill_related_selections(self, opts=None, root_alias=None, cur_depth=1,
  • tests/regressiontests/aggregation_regress/tests.py

     
     1from django.conf import settings
    12from django.test import TestCase
    2 from django.db.models import Max
     3from django.db import DEFAULT_DB_ALIAS
     4from django.db.models import Count, Max
    35
    46from regressiontests.aggregation_regress.models import *
    57
     
    4648        qs1 = books.filter(id__in=qs)
    4749        qs2 = books.filter(id__in=list(qs))
    4850        self.assertEqual(list(qs1), list(qs2))
     51
     52    def test_annotate_with_extra(self):
     53        """
     54        Regression test for #11916: Extra params + aggregation creates
     55        incorrect SQL.
     56        """
     57        #oracle doesn't support subqueries in group by clause
     58        if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] == 'django.db.backends.oracle':
     59            return
     60        shortest_book_sql = """
     61        SELECT name
     62        FROM aggregation_regress_book b
     63        WHERE b.publisher_id = aggregation_regress_publisher.id
     64        ORDER BY b.pages
     65        LIMIT 1
     66        """
     67        # tests that this query does not raise a DatabaseError due to the full
     68        # subselect being (erroneously) added to the GROUP BY parameters
     69        qs = Publisher.objects.extra(select={
     70            'name_of_shortest_book': shortest_book_sql,
     71        }).annotate(total_books=Count('book'))
     72        # force execution of the query
     73        list(qs)
Back to Top