From 3a9a4570efc933a50efec27bdd06af11877eeac6 Mon Sep 17 00:00:00 2001 From: Shai Berger Date: Sun, 30 Mar 2014 20:03:35 +0300 Subject: [1.7.x] Fixed #22343 -- Disallowed select_for_update in autocommit mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The ticket was originally about two failing tests, which are fixed by putting their queries in transactions. Thanks Tim Graham for the report, Aymeric Augustin for the fix, and Simon Charette, Tim Graham & Loïc Bistuer for review. Backport of b990df1d63 from master --- django/db/models/sql/compiler.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'django/db/models/sql') diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index e166492896..9351169357 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -11,6 +11,7 @@ from django.db.models.sql.constants import (CURSOR, SINGLE, MULTI, NO_RESULTS, from django.db.models.sql.datastructures import EmptyResultSet from django.db.models.sql.expressions import SQLEvaluator from django.db.models.sql.query import get_order_dir, Query +from django.db.transaction import TransactionManagementError from django.db.utils import DatabaseError from django.utils import six from django.utils.six.moves import zip @@ -157,6 +158,9 @@ class SQLCompiler(object): result.append('OFFSET %d' % self.query.low_mark) if self.query.select_for_update and self.connection.features.has_select_for_update: + if self.connection.get_autocommit(): + raise TransactionManagementError("select_for_update cannot be used outside of a transaction.") + # If we've been asked for a NOWAIT query but the backend does not support it, # raise a DatabaseError otherwise we could get an unexpected deadlock. nowait = self.query.select_for_update_nowait -- cgit v1.3