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 | |
| 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
| -rw-r--r-- | django/db/backends/base/schema.py | 4 | ||||
| -rw-r--r-- | docs/releases/1.8.3.txt | 3 | ||||
| -rw-r--r-- | tests/schema/models.py | 1 | ||||
| -rw-r--r-- | tests/schema/tests.py | 39 |
4 files changed, 46 insertions, 1 deletions
diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py index 3a78fb2df4..be5c0f7c9a 100644 --- a/django/db/backends/base/schema.py +++ b/django/db/backends/base/schema.py @@ -659,7 +659,9 @@ class BaseDatabaseSchemaEditor(object): for sql, params in post_actions: self.execute(sql, params) # Added a unique? - if not old_field.unique and new_field.unique: + if (not old_field.unique and new_field.unique) or ( + old_field.primary_key and not new_field.primary_key and new_field.unique + ): self.execute(self._create_unique_sql(model, [new_field.column])) # Added an index? if (not old_field.db_index and new_field.db_index and diff --git a/docs/releases/1.8.3.txt b/docs/releases/1.8.3.txt index a45ba7d268..c2a4f6af3d 100644 --- a/docs/releases/1.8.3.txt +++ b/docs/releases/1.8.3.txt @@ -34,3 +34,6 @@ Bugfixes * Fixed quoting of SQL when renaming a field to ``AutoField`` in PostgreSQL (:ticket:`24892`). + +* Fixed lack of unique constraint when changing a field from + ``primary_key=True`` to ``unique=True`` (:ticket:`24893`). 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 |
