Permalink
Browse files

Fixed #22434 -- Retain ordering on related sliced subqueries.

Thanks maciej.pawlisz for the report, and charettes for the review.
  • Loading branch information...
justhamade authored and charettes committed Apr 14, 2014
1 parent 7cd74fc commit a13df671a5a5b867397ce05f73a59a3f4504868a
Showing with 43 additions and 42 deletions.
  1. +3 −1 django/db/models/sql/where.py
  2. +40 −41 tests/queries/tests.py
@@ -418,7 +418,9 @@ def as_sql(self, qn, connection):
else:
query = query._clone()
query = query.query
- query.clear_ordering(True)
+ if query.can_filter():
+ # If there is no slicing in use, then we can safely drop all ordering
+ query.clear_ordering(True)
query_compiler = query.get_compiler(connection=connection)
return query_compiler.as_subquery_condition(self.alias, self.columns, qn)
View
@@ -8,7 +8,7 @@
import warnings
from django.core.exceptions import FieldError
-from django.db import DatabaseError, connection, connections, DEFAULT_DB_ALIAS
+from django.db import connection, DEFAULT_DB_ALIAS
from django.db.models import Count, F, Q
from django.db.models.sql.where import WhereNode, EverythingNode, NothingNode
from django.db.models.sql.datastructures import EmptyResultSet
@@ -1934,6 +1934,7 @@ def test_annotated_ordering(self):
self.assertEqual(qs.order_by('num_notes').ordered, True)
+@skipUnlessDBFeature('allow_sliced_subqueries')
class SubqueryTests(TestCase):
def setUp(self):
DumbCategory.objects.create(id=1)
@@ -1943,59 +1944,57 @@ def setUp(self):
def test_ordered_subselect(self):
"Subselects honor any manual ordering"
- try:
- query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[0:2])
- self.assertEqual(set(query.values_list('id', flat=True)), set([3, 4]))
+ query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[0:2])
+ self.assertEqual(set(query.values_list('id', flat=True)), set([3, 4]))
- query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[:2])
- self.assertEqual(set(query.values_list('id', flat=True)), set([3, 4]))
+ query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[:2])
+ self.assertEqual(set(query.values_list('id', flat=True)), set([3, 4]))
- query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[1:2])
- self.assertEqual(set(query.values_list('id', flat=True)), set([3]))
+ query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[1:2])
+ self.assertEqual(set(query.values_list('id', flat=True)), set([3]))
- query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[2:])
- self.assertEqual(set(query.values_list('id', flat=True)), set([1, 2]))
- except DatabaseError as e:
- # Oracle and MySQL both have problems with sliced subselects.
- # This prevents us from even evaluating this test case at all.
- # Refs #10099
- self.assertFalse(connections[DEFAULT_DB_ALIAS].features.allow_sliced_subqueries, str(e))
+ query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[2:])
+ self.assertEqual(set(query.values_list('id', flat=True)), set([1, 2]))
def test_slice_subquery_and_query(self):
"""
Slice a query that has a sliced subquery
"""
- try:
- query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[0:2])[0:2]
- self.assertEqual(set([x.id for x in query]), set([3, 4]))
+ query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[0:2])[0:2]
+ self.assertEqual(set([x.id for x in query]), set([3, 4]))
+
+ query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[1:3])[1:3]
+ self.assertEqual(set([x.id for x in query]), set([3]))
- query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[1:3])[1:3]
- self.assertEqual(set([x.id for x in query]), set([3]))
+ query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[2:])[1:]
+ self.assertEqual(set([x.id for x in query]), set([2]))
+
+ def test_related_sliced_subquery(self):
+ """
+ Related objects constraints can safely contain sliced subqueries.
+ refs #22434
+ """
+ generic = NamedCategory.objects.create(name="Generic")
+ t1 = Tag.objects.create(name='t1', category=generic)
+ t2 = Tag.objects.create(name='t2', category=generic)
+ ManagedModel.objects.create(data='mm1', tag=t1, public=True)
+ mm2 = ManagedModel.objects.create(data='mm2', tag=t2, public=True)
- query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[2:])[1:]
- self.assertEqual(set([x.id for x in query]), set([2]))
- except DatabaseError as e:
- # Oracle and MySQL both have problems with sliced subselects.
- # This prevents us from even evaluating this test case at all.
- # Refs #10099
- self.assertFalse(connections[DEFAULT_DB_ALIAS].features.allow_sliced_subqueries, str(e))
+ query = ManagedModel.normal_manager.filter(
+ tag__in=Tag.objects.order_by('-id')[:1]
+ )
+ self.assertEqual(set([x.id for x in query]), set([mm2.id]))
def test_sliced_delete(self):
"Delete queries can safely contain sliced subqueries"
- try:
- DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[0:1]).delete()
- self.assertEqual(set(DumbCategory.objects.values_list('id', flat=True)), set([1, 2, 3]))
-
- DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[1:2]).delete()
- self.assertEqual(set(DumbCategory.objects.values_list('id', flat=True)), set([1, 3]))
-
- DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[1:]).delete()
- self.assertEqual(set(DumbCategory.objects.values_list('id', flat=True)), set([3]))
- except DatabaseError as e:
- # Oracle and MySQL both have problems with sliced subselects.
- # This prevents us from even evaluating this test case at all.
- # Refs #10099
- self.assertFalse(connections[DEFAULT_DB_ALIAS].features.allow_sliced_subqueries, str(e))
+ DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[0:1]).delete()
+ self.assertEqual(set(DumbCategory.objects.values_list('id', flat=True)), set([1, 2, 3]))
+
+ DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[1:2]).delete()
+ self.assertEqual(set(DumbCategory.objects.values_list('id', flat=True)), set([1, 3]))
+
+ DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[1:]).delete()
+ self.assertEqual(set(DumbCategory.objects.values_list('id', flat=True)), set([3]))
class CloneTests(TestCase):

0 comments on commit a13df67

Please sign in to comment.