summaryrefslogtreecommitdiff
path: root/tests/schema
diff options
context:
space:
mode:
authorJeremy Bowman <jbowman@edx.org>2018-04-09 13:35:06 -0400
committerTim Graham <timograham@gmail.com>2018-04-11 23:17:11 -0400
commitee17bb8a67a9e7e688da6e6f4b3be1b3a69c09b0 (patch)
treeccb5560ac1a68905e084ea84b9c8b511273ef674 /tests/schema
parent003334f8af29e2023cf7ad7d080aa9ab26a7c528 (diff)
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.
Diffstat (limited to 'tests/schema')
-rw-r--r--tests/schema/tests.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index 12c33a8433..b535a834f9 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -1546,6 +1546,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):
"""