summaryrefslogtreecommitdiff
path: root/django/db/backends/sqlite3/base.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/db/backends/sqlite3/base.py')
-rw-r--r--django/db/backends/sqlite3/base.py12
1 files changed, 7 insertions, 5 deletions
diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py
index 6ead2b2d48..9b0be56862 100644
--- a/django/db/backends/sqlite3/base.py
+++ b/django/db/backends/sqlite3/base.py
@@ -215,11 +215,13 @@ class DatabaseWrapper(BaseDatabaseWrapper):
self.connection.isolation_level = level
def disable_constraint_checking(self):
- if self.in_atomic_block:
- # sqlite3 cannot disable constraint checking inside a transaction.
- return False
- self.cursor().execute('PRAGMA foreign_keys = OFF')
- return True
+ with self.cursor() as cursor:
+ cursor.execute('PRAGMA foreign_keys = OFF')
+ # Foreign key constraints cannot be turned off while in a multi-
+ # statement transaction. Fetch the current state of the pragma
+ # to determine if constraints are effectively disabled.
+ enabled = cursor.execute('PRAGMA foreign_keys').fetchone()[0]
+ return not bool(enabled)
def enable_constraint_checking(self):
self.cursor().execute('PRAGMA foreign_keys = ON')