Code

Ticket #11916: ticket_11916.2.diff

File ticket_11916.2.diff, 1.8 KB (added by paluh, 4 years ago)

patch + test

Line 
1Index: django/db/models/sql/compiler.py
2===================================================================
3--- django/db/models/sql/compiler.py    (revision 12824)
4+++ django/db/models/sql/compiler.py    (working copy)
5@@ -484,7 +484,7 @@
6                 elif hasattr(col, 'as_sql'):
7                     result.append(col.as_sql(qn))
8                 else:
9-                    result.append(str(col))
10+                    result.append('(%s)' % str(col))
11         return result, params
12 
13     def fill_related_selections(self, opts=None, root_alias=None, cur_depth=1,
14Index: tests/regressiontests/aggregation_regress/models.py
15===================================================================
16--- tests/regressiontests/aggregation_regress/models.py (revision 12588)
17+++ tests/regressiontests/aggregation_regress/models.py (working copy)
18@@ -318,6 +318,19 @@
19 ...
20 FieldError: Cannot compute Avg('mean_age'): 'mean_age' is an aggregate
21 
22+# Regression for 11916 - extra params + aggregation creates incorrect SQL
23+>>> shortest_book_sql = ' \\
24+... SELECT name \\
25+... FROM aggregation_regress_book b \\
26+... WHERE b.publisher_id = aggregation_regress_publisher.id \\
27+... ORDER BY b.pages \\
28+... LIMIT 1 '
29+>>> # tests that this query does not raise a DatabaseError due to the full
30+>>> # subselect being (erroneously) added to the GROUP BY parameters
31+>>> Publisher.objects.extra(select={
32+...     'name_of_shortest_book': shortest_book_sql,
33+... }).annotate(total_books=Count('book')).order_by('name').values_list('id', 'total_books', 'name_of_shortest_book')
34+[(1, 2, u'Practical Django Projects'), (5, 0, None), (4, 1, u'Paradigms of Artificial Intelligence Programming: Case Studies in Common Lisp'), (3, 2, u'Python Web Development with Django'), (2, 1, u'Sams Teach Yourself Django in 24 Hours')]
35 """
36 }
37