diff options
| author | Simon Charette <charette.s@gmail.com> | 2018-12-07 19:55:47 -0500 |
|---|---|---|
| committer | Simon Charette <charette.s@gmail.com> | 2018-12-17 10:01:35 -0500 |
| commit | 7cf9d15cf80df4ed9a1179bc457ff2d03b5e5cb3 (patch) | |
| tree | 986b4ebe928047d827867cc22e7a70f141fa828e /django/db/backends/sqlite3/schema.py | |
| parent | 8d741bd88fa6bd14327f6fa791017d0773b41cf2 (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/schema.py')
| -rw-r--r-- | django/db/backends/sqlite3/schema.py | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py index e0d8135cbd..0954c66aaf 100644 --- a/django/db/backends/sqlite3/schema.py +++ b/django/db/backends/sqlite3/schema.py @@ -19,8 +19,15 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): def __enter__(self): # Some SQLite schema alterations need foreign key constraints to be - # disabled. Enforce it here for the duration of the transaction. - self.connection.disable_constraint_checking() + # disabled. Enforce it here for the duration of the schema edition. + if not self.connection.disable_constraint_checking(): + raise NotSupportedError( + 'SQLite schema editor cannot be used while foreign key ' + 'constraint checks are enabled. Make sure to disable them ' + 'before entering a transaction.atomic() context because ' + 'SQLite3 does not support disabling them in the middle of ' + 'a multi-statement transaction.' + ) self.connection.cursor().execute('PRAGMA legacy_alter_table = ON') return super().__enter__() |
