[1.7.x] Fixed #22434 -- Retain ordering on related sliced subqueries.
Thanks maciej.pawlisz for the report, and charettes for the review.
Backport of a13df671a5
from master
This commit is contained in:
parent
841e21a85c
commit
7f8bd1a4b3
|
@ -418,7 +418,9 @@ class SubqueryConstraint(object):
|
||||||
else:
|
else:
|
||||||
query = query._clone()
|
query = query._clone()
|
||||||
query = query.query
|
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)
|
query_compiler = query.get_compiler(connection=connection)
|
||||||
return query_compiler.as_subquery_condition(self.alias, self.columns, qn)
|
return query_compiler.as_subquery_condition(self.alias, self.columns, qn)
|
||||||
|
|
|
@ -8,7 +8,7 @@ import unittest
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
from django.core.exceptions import FieldError
|
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 import Count, F, Q
|
||||||
from django.db.models.sql.where import WhereNode, EverythingNode, NothingNode
|
from django.db.models.sql.where import WhereNode, EverythingNode, NothingNode
|
||||||
from django.db.models.sql.datastructures import EmptyResultSet
|
from django.db.models.sql.datastructures import EmptyResultSet
|
||||||
|
@ -1933,6 +1933,7 @@ class QuerysetOrderedTests(unittest.TestCase):
|
||||||
self.assertEqual(qs.order_by('num_notes').ordered, True)
|
self.assertEqual(qs.order_by('num_notes').ordered, True)
|
||||||
|
|
||||||
|
|
||||||
|
@skipUnlessDBFeature('allow_sliced_subqueries')
|
||||||
class SubqueryTests(TestCase):
|
class SubqueryTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
DumbCategory.objects.create(id=1)
|
DumbCategory.objects.create(id=1)
|
||||||
|
@ -1942,59 +1943,57 @@ class SubqueryTests(TestCase):
|
||||||
|
|
||||||
def test_ordered_subselect(self):
|
def test_ordered_subselect(self):
|
||||||
"Subselects honor any manual ordering"
|
"Subselects honor any manual ordering"
|
||||||
try:
|
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[0:2])
|
||||||
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]))
|
||||||
self.assertEqual(set(query.values_list('id', flat=True)), set([3, 4]))
|
|
||||||
|
|
||||||
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[:2])
|
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[:2])
|
||||||
self.assertEqual(set(query.values_list('id', flat=True)), set([3, 4]))
|
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])
|
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[1:2])
|
||||||
self.assertEqual(set(query.values_list('id', flat=True)), set([3]))
|
self.assertEqual(set(query.values_list('id', flat=True)), set([3]))
|
||||||
|
|
||||||
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[2:])
|
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[2:])
|
||||||
self.assertEqual(set(query.values_list('id', flat=True)), set([1, 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))
|
|
||||||
|
|
||||||
def test_slice_subquery_and_query(self):
|
def test_slice_subquery_and_query(self):
|
||||||
"""
|
"""
|
||||||
Slice a query that has a sliced subquery
|
Slice a query that has a sliced subquery
|
||||||
"""
|
"""
|
||||||
try:
|
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[0:2])[0:2]
|
||||||
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]))
|
||||||
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]
|
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]))
|
self.assertEqual(set([x.id for x in query]), set([3]))
|
||||||
|
|
||||||
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[2:])[1:]
|
query = DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[2:])[1:]
|
||||||
self.assertEqual(set([x.id for x in query]), set([2]))
|
self.assertEqual(set([x.id for x in query]), set([2]))
|
||||||
except DatabaseError as e:
|
|
||||||
# Oracle and MySQL both have problems with sliced subselects.
|
def test_related_sliced_subquery(self):
|
||||||
# This prevents us from even evaluating this test case at all.
|
"""
|
||||||
# Refs #10099
|
Related objects constraints can safely contain sliced subqueries.
|
||||||
self.assertFalse(connections[DEFAULT_DB_ALIAS].features.allow_sliced_subqueries, str(e))
|
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 = 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):
|
def test_sliced_delete(self):
|
||||||
"Delete queries can safely contain sliced subqueries"
|
"Delete queries can safely contain sliced subqueries"
|
||||||
try:
|
DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[0:1]).delete()
|
||||||
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]))
|
||||||
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()
|
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]))
|
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()
|
DumbCategory.objects.filter(id__in=DumbCategory.objects.order_by('-id')[1:]).delete()
|
||||||
self.assertEqual(set(DumbCategory.objects.values_list('id', flat=True)), set([3]))
|
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))
|
|
||||||
|
|
||||||
|
|
||||||
class CloneTests(TestCase):
|
class CloneTests(TestCase):
|
||||||
|
|
Loading…
Reference in New Issue