summaryrefslogtreecommitdiff
path: root/tests
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:24:42 -0400
commit8f76939f54924b01b41d4242e71ca98eb35964f2 (patch)
tree60b2468c97a032bbf79605d618cfabac2717b6f5 /tests
parenta1f4e14f99dfaa63b181c2fb1f211d9f91644ad0 (diff)
[1.11.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')
-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 36daccb19b..1f8f97188c 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -1393,6 +1393,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):
"""