diff options
| author | Jeremy Bowman <jbowman@edx.org> | 2018-04-09 13:35:06 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-04-11 23:17:37 -0400 |
| commit | d5018abf1c4e3c68f1a825d05eb9035325707e16 (patch) | |
| tree | 3dca46778beda4ca2ca000f472043d9adfd6da4b /tests/schema/tests.py | |
| parent | 95e1191690d2448609b4d0a1eb0b7c822e8a4ac8 (diff) | |
[2.0.x] Fixed #29193 -- Prevented unnecessary foreign key drops when altering a unique field.
Stopped dropping and recreating foreign key constraints on other fields
in the same table as the one which is actually being altered in an
AlterField operation.
Regression in c3e0adcad8d8ba94b33cabd137056166ed36dae0.
Backport of ee17bb8a67a9e7e688da6e6f4b3be1b3a69c09b0 from master
Diffstat (limited to 'tests/schema/tests.py')
| -rw-r--r-- | tests/schema/tests.py | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py index cd7e4f638c..6046e4473d 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -1539,6 +1539,40 @@ class SchemaTests(TransactionTestCase): Tag.objects.all().delete() @isolate_apps('schema') + @unittest.skipIf(connection.vendor == 'sqlite', 'SQLite naively remakes the table on field alteration.') + @skipUnlessDBFeature('supports_foreign_keys') + def test_unique_no_unnecessary_fk_drops(self): + """ + If AlterField isn't selective about dropping foreign key constraints + when modifying a field with a unique constraint, the AlterField + incorrectly drops and recreates the Book.author foreign key even though + it doesn't restrict the field being changed (#29193). + """ + class Author(Model): + name = CharField(max_length=254, unique=True) + + class Meta: + app_label = 'schema' + + class Book(Model): + author = ForeignKey(Author, CASCADE) + + class Meta: + app_label = 'schema' + + with connection.schema_editor() as editor: + editor.create_model(Author) + editor.create_model(Book) + new_field = CharField(max_length=255, unique=True) + new_field.model = Author + new_field.set_attributes_from_name('name') + with patch_logger('django.db.backends.schema', 'debug') as logger_calls: + with connection.schema_editor() as editor: + editor.alter_field(Author, Author._meta.get_field('name'), new_field) + # One SQL statement is executed to alter the field. + self.assertEqual(len(logger_calls), 1) + + @isolate_apps('schema') @unittest.skipIf(connection.vendor == 'sqlite', 'SQLite remakes the table on field alteration.') def test_unique_and_reverse_m2m(self): """ |
