mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Fixed #27193 -- Preserved ordering in select_for_update subqueries.
This commit is contained in:
parent
9e26a8331d
commit
8ac115c730
@ -476,8 +476,11 @@ class SQLCompiler(object):
|
|||||||
Used when nesting this query inside another.
|
Used when nesting this query inside another.
|
||||||
"""
|
"""
|
||||||
obj = self.query.clone()
|
obj = self.query.clone()
|
||||||
if obj.low_mark == 0 and obj.high_mark is None and not self.query.distinct_fields:
|
# It's safe to drop ordering if the queryset isn't using slicing,
|
||||||
# If there is no slicing in use, then we can safely drop all ordering
|
# distinct(*fields) or select_for_update().
|
||||||
|
if (obj.low_mark == 0 and obj.high_mark is None and
|
||||||
|
not self.query.distinct_fields and
|
||||||
|
not self.query.select_for_update):
|
||||||
obj.clear_ordering(True)
|
obj.clear_ordering(True)
|
||||||
nested_sql = obj.get_compiler(connection=self.connection).as_sql(subquery=True)
|
nested_sql = obj.get_compiler(connection=self.connection).as_sql(subquery=True)
|
||||||
if nested_sql == ('', ()):
|
if nested_sql == ('', ()):
|
||||||
|
@ -300,3 +300,12 @@ class SelectForUpdateTests(TransactionTestCase):
|
|||||||
def test_nowait_and_skip_locked(self):
|
def test_nowait_and_skip_locked(self):
|
||||||
with self.assertRaisesMessage(ValueError, 'The nowait option cannot be used with skip_locked.'):
|
with self.assertRaisesMessage(ValueError, 'The nowait option cannot be used with skip_locked.'):
|
||||||
Person.objects.select_for_update(nowait=True, skip_locked=True)
|
Person.objects.select_for_update(nowait=True, skip_locked=True)
|
||||||
|
|
||||||
|
def test_ordered_select_for_update(self):
|
||||||
|
"""
|
||||||
|
Subqueries should respect ordering as an ORDER BY clause may be useful
|
||||||
|
to specify a row locking order to prevent deadlocks (#27193).
|
||||||
|
"""
|
||||||
|
with transaction.atomic():
|
||||||
|
qs = Person.objects.filter(id__in=Person.objects.order_by('-id').select_for_update())
|
||||||
|
self.assertIn('ORDER BY', str(qs.query))
|
||||||
|
Loading…
Reference in New Issue
Block a user