summaryrefslogtreecommitdiff
path: root/tests/schema
diff options
context:
space:
mode:
authorTim Martin <tim@asymptotic.co.uk>2017-09-18 20:54:48 +0100
committerTim Graham <timograham@gmail.com>2018-01-13 20:11:55 -0500
commit02365d3f38a64a5c2f3e932f23925a381d5bb151 (patch)
tree212d7d35f120a63e06e6fcb56570fc9cff2b8a2d /tests/schema
parent56e590cc0be4d8c8b6fe0967583a6e02d18ee03e (diff)
downloaddjango-02365d3f38a64a5c2f3e932f23925a381d5bb151.tar.gz
Fixed #28542 -- Fixed deletion of primary key constraint if the new field is unique.
Diffstat (limited to 'tests/schema')
-rw-r--r--tests/schema/tests.py44
1 files changed, 30 insertions, 14 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index cc2d5384f8..3a66c2ae7a 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -1098,34 +1098,50 @@ class SchemaTests(TransactionTestCase):
Should be able to rename an IntegerField(primary_key=True) to
IntegerField(unique=True).
"""
- class IntegerUnique(Model):
+ with connection.schema_editor() as editor:
+ editor.create_model(IntegerPK)
+ # Delete the old PK
+ old_field = IntegerPK._meta.get_field('i')
+ new_field = IntegerField(unique=True)
+ new_field.model = IntegerPK
+ new_field.set_attributes_from_name('i')
+ with connection.schema_editor() as editor:
+ editor.alter_field(IntegerPK, old_field, new_field, strict=True)
+ # The primary key constraint is gone. Result depends on database:
+ # 'id' for SQLite, None for others (must not be 'i').
+ self.assertIn(self.get_primary_key(IntegerPK._meta.db_table), ('id', None))
+
+ # Set up a model class as it currently stands. The original IntegerPK
+ # class is now out of date and some backends make use of the whole
+ # model class when modifying a field (such as sqlite3 when remaking a
+ # table) so an outdated model class leads to incorrect results.
+ class Transitional(Model):
i = IntegerField(unique=True)
- j = IntegerField(primary_key=True)
+ j = IntegerField(unique=True)
class Meta:
app_label = 'schema'
apps = new_apps
db_table = 'INTEGERPK'
- with connection.schema_editor() as editor:
- editor.create_model(IntegerPK)
-
# model requires a new PK
- old_field = IntegerPK._meta.get_field('j')
+ old_field = Transitional._meta.get_field('j')
new_field = IntegerField(primary_key=True)
- new_field.model = IntegerPK
+ new_field.model = Transitional
new_field.set_attributes_from_name('j')
with connection.schema_editor() as editor:
- editor.alter_field(IntegerPK, old_field, new_field, strict=True)
+ editor.alter_field(Transitional, old_field, new_field, strict=True)
- old_field = IntegerPK._meta.get_field('i')
- new_field = IntegerField(unique=True)
- new_field.model = IntegerPK
- new_field.set_attributes_from_name('i')
+ # Create a model class representing the updated model.
+ class IntegerUnique(Model):
+ i = IntegerField(unique=True)
+ j = IntegerField(primary_key=True)
- with connection.schema_editor() as editor:
- editor.alter_field(IntegerPK, old_field, new_field, strict=True)
+ class Meta:
+ app_label = 'schema'
+ apps = new_apps
+ db_table = 'INTEGERPK'
# Ensure unique constraint works.
IntegerUnique.objects.create(i=1, j=1)