[1.6.x] Fixed #19918 -- Modified select_for_update to run on the write database.
Backport of 1c64a0f29e
from master
This commit is contained in:
parent
363b81c59c
commit
80b95a2b1f
|
@ -635,6 +635,7 @@ class QuerySet(object):
|
||||||
# Default to false for nowait
|
# Default to false for nowait
|
||||||
nowait = kwargs.pop('nowait', False)
|
nowait = kwargs.pop('nowait', False)
|
||||||
obj = self._clone()
|
obj = self._clone()
|
||||||
|
obj._for_write = True
|
||||||
obj.query.select_for_update = True
|
obj.query.select_for_update = True
|
||||||
obj.query.select_for_update_nowait = nowait
|
obj.query.select_for_update_nowait = nowait
|
||||||
return obj
|
return obj
|
||||||
|
|
|
@ -4,12 +4,14 @@ import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db import transaction, connection
|
from django.db import transaction, connection, router
|
||||||
from django.db.utils import ConnectionHandler, DEFAULT_DB_ALIAS, DatabaseError
|
from django.db.utils import ConnectionHandler, DEFAULT_DB_ALIAS, DatabaseError
|
||||||
from django.test import (TransactionTestCase, skipIfDBFeature,
|
from django.test import (TransactionTestCase, skipIfDBFeature,
|
||||||
skipUnlessDBFeature)
|
skipUnlessDBFeature)
|
||||||
from django.utils import unittest
|
from django.utils import unittest
|
||||||
|
|
||||||
|
from multiple_database.tests import TestRouter
|
||||||
|
|
||||||
from .models import Person
|
from .models import Person
|
||||||
|
|
||||||
# Some tests require threading, which might not be available. So create a
|
# Some tests require threading, which might not be available. So create a
|
||||||
|
@ -268,3 +270,13 @@ class SelectForUpdateTests(TransactionTestCase):
|
||||||
"""
|
"""
|
||||||
people = list(Person.objects.select_for_update())
|
people = list(Person.objects.select_for_update())
|
||||||
self.assertTrue(transaction.is_dirty())
|
self.assertTrue(transaction.is_dirty())
|
||||||
|
|
||||||
|
@skipUnlessDBFeature('has_select_for_update')
|
||||||
|
def test_select_for_update_on_multidb(self):
|
||||||
|
old_routers = router.routers
|
||||||
|
try:
|
||||||
|
router.routers = [TestRouter()]
|
||||||
|
query = Person.objects.select_for_update()
|
||||||
|
self.assertEqual(router.db_for_write(Person), query.db)
|
||||||
|
finally:
|
||||||
|
router.routers = old_routers
|
||||||
|
|
Loading…
Reference in New Issue