Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1.1.X] Fixed #12822: Don't copy the _aggregate_select_cache when clo…
…ning a query set, as that can lead to incorrect SQL being generated for the query. Thanks to mat for the report and test, tobias for the fix, and Alex for review. r12830 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12831 bcc190cf-cafb-0310-a4f2-bffc1f526a37
- Loading branch information
Showing
2 changed files
with
54 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,48 @@ | |||
from django.test import TestCase | |||
from django.db.models import Max | |||
|
|||
from regressiontests.aggregation_regress.models import * | |||
|
|||
|
|||
class AggregationTests(TestCase): | |||
|
|||
def test_aggregates_in_where_clause(self): | |||
""" | |||
Regression test for #12822: DatabaseError: aggregates not allowed in | |||
WHERE clause | |||
Tests that the subselect works and returns results equivalent to a | |||
query with the IDs listed. | |||
Before the corresponding fix for this bug, this test passed in 1.1 and | |||
failed in 1.2-beta (trunk). | |||
""" | |||
qs = Book.objects.values('contact').annotate(Max('id')) | |||
qs = qs.order_by('contact').values_list('id__max', flat=True) | |||
# don't do anything with the queryset (qs) before including it as a | |||
# subquery | |||
books = Book.objects.order_by('id') | |||
qs1 = books.filter(id__in=qs) | |||
qs2 = books.filter(id__in=list(qs)) | |||
self.assertEqual(list(qs1), list(qs2)) | |||
|
|||
def test_aggregates_in_where_clause_pre_eval(self): | |||
""" | |||
Regression test for #12822: DatabaseError: aggregates not allowed in | |||
WHERE clause | |||
Same as the above test, but evaluates the queryset for the subquery | |||
before it's used as a subquery. | |||
Before the corresponding fix for this bug, this test failed in both | |||
1.1 and 1.2-beta (trunk). | |||
""" | |||
qs = Book.objects.values('contact').annotate(Max('id')) | |||
qs = qs.order_by('contact').values_list('id__max', flat=True) | |||
# force the queryset (qs) for the subquery to be evaluated in its | |||
# current state | |||
list(qs) | |||
books = Book.objects.order_by('id') | |||
qs1 = books.filter(id__in=qs) | |||
qs2 = books.filter(id__in=list(qs)) | |||
self.assertEqual(list(qs1), list(qs2)) |