summaryrefslogtreecommitdiff
path: root/django/db/backends/sqlite3/base.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2018-12-07 19:55:47 -0500
committerSimon Charette <charette.s@gmail.com>2018-12-17 10:01:35 -0500
commit7cf9d15cf80df4ed9a1179bc457ff2d03b5e5cb3 (patch)
tree986b4ebe928047d827867cc22e7a70f141fa828e /django/db/backends/sqlite3/base.py
parent8d741bd88fa6bd14327f6fa791017d0773b41cf2 (diff)
[2.1.x] Fixed #30023 -- Prevented SQLite schema alterations while foreign key checks are enabled.
Prior to this change foreign key constraint references could be left pointing at tables dropped during operations simulating unsupported table alterations because of an unexpected failure to disable foreign key constraint checks. SQLite3 does not allow disabling such checks while in a transaction so they must be disabled beforehand. Thanks ezaquarii for the report and Carlton and Tim for the review. Backport of 315357ad25a6590e7f4564ec2e56a22132b09001 from master.
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')