diff options
| author | Tim Graham <timograham@gmail.com> | 2015-06-01 19:27:28 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-06-02 09:32:22 -0400 |
| commit | 1c57d7e7faaeadb7f32e01586da7997d11df1ad9 (patch) | |
| tree | 0f3b6ffff286571b1172abb3c6d858a31589703e /tests | |
| parent | 8911d2e20fc5f4fc7b0274594813ed0b1ad8bc6b (diff) | |
[1.8.x] Fixed #24893 -- Fixed lack of unique constraint when changing a field from primary_key=True to unique=True
Backport of e1e6399c2ce39a3656155c4d704340eac83926a7 from master
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/schema/models.py | 1 | ||||
| -rw-r--r-- | tests/schema/tests.py | 39 |
2 files changed, 40 insertions, 0 deletions
diff --git a/tests/schema/models.py b/tests/schema/models.py index ab5dd6087b..4b37fcec37 100644 --- a/tests/schema/models.py +++ b/tests/schema/models.py @@ -82,6 +82,7 @@ class BookWithSlug(models.Model): class IntegerPK(models.Model): i = models.IntegerField(primary_key=True) + j = models.IntegerField(unique=True) class Meta: apps = new_apps diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 05cb26a414..23a79dd4bd 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -745,6 +745,45 @@ class SchemaTests(TransactionTestCase): with connection.schema_editor() as editor: editor.alter_field(IntegerPK, old_field, new_field, strict=True) + def test_alter_int_pk_to_int_unique(self): + """ + Should be able to rename an IntegerField(primary_key=True) to + IntegerField(unique=True). + """ + class IntegerUnique(Model): + i = IntegerField(unique=True) + j = IntegerField(primary_key=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') + new_field = IntegerField(primary_key=True) + new_field.model = IntegerPK + new_field.set_attributes_from_name('j') + + with connection.schema_editor() as editor: + editor.alter_field(IntegerPK, 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') + + with connection.schema_editor() as editor: + editor.alter_field(IntegerPK, old_field, new_field, strict=True) + + # Ensure unique constraint works. + IntegerUnique.objects.create(i=1, j=1) + with self.assertRaises(IntegrityError): + IntegerUnique.objects.create(i=1, j=2) + def test_rename(self): """ Tests simple altering of fields |
