From 24f1d8e2175154e6c16bbcdd3e7d8978d55fd29c Mon Sep 17 00:00:00 2001 From: Michiya Takahashi Date: Sat, 16 Aug 2014 19:07:08 +0900 Subject: [PATCH] add transaction management for select for update --- sql_server/pyodbc/compiler.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sql_server/pyodbc/compiler.py b/sql_server/pyodbc/compiler.py index 0933d7a2..7e3bd44f 100644 --- a/sql_server/pyodbc/compiler.py +++ b/sql_server/pyodbc/compiler.py @@ -4,6 +4,7 @@ from itertools import izip_longest as zip_longest from django.db.models.sql import compiler +from django.db.transaction import TransactionManagementError from django.utils import six from sql_server.pyodbc.aggregates import AggregateWrapper @@ -108,6 +109,9 @@ def as_sql(self, with_limits=True, with_col_aliases=False): params.extend(f_params) 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