From 80b95a2b1fc8c8425a81a23706af971061dc238f Mon Sep 17 00:00:00 2001 From: Alex Cucu Date: Tue, 26 Feb 2013 11:51:56 +0200 Subject: [PATCH] [1.6.x] Fixed #19918 -- Modified select_for_update to run on the write database. Backport of 1c64a0f29e from master --- django/db/models/query.py | 1 + tests/select_for_update/tests.py | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/django/db/models/query.py b/django/db/models/query.py index 27a87a3f65bcb..31b79ed0a2c03 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -635,6 +635,7 @@ def select_for_update(self, **kwargs): # Default to false for nowait nowait = kwargs.pop('nowait', False) obj = self._clone() + obj._for_write = True obj.query.select_for_update = True obj.query.select_for_update_nowait = nowait return obj diff --git a/tests/select_for_update/tests.py b/tests/select_for_update/tests.py index f087ca123af54..edef455154df0 100644 --- a/tests/select_for_update/tests.py +++ b/tests/select_for_update/tests.py @@ -4,12 +4,14 @@ import time 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.test import (TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature) from django.utils import unittest +from multiple_database.tests import TestRouter + from .models import Person # Some tests require threading, which might not be available. So create a @@ -268,3 +270,13 @@ def test_transaction_dirty_managed(self): """ people = list(Person.objects.select_for_update()) 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