From 0b013951086814ebee048f0fa9a620e03f891494 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Sat, 18 Aug 2012 14:00:42 +0100 Subject: [PATCH] Test that unique constraints get ported with column rename --- tests/modeltests/schema/models.py | 9 +++++++++ tests/modeltests/schema/tests.py | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/modeltests/schema/models.py b/tests/modeltests/schema/models.py index e9eba1fc6f..053aa026f7 100644 --- a/tests/modeltests/schema/models.py +++ b/tests/modeltests/schema/models.py @@ -37,6 +37,15 @@ class Tag(models.Model): managed = False +class TagUniqueRename(models.Model): + title = models.CharField(max_length=255) + slug2 = models.SlugField(unique=True) + + class Meta: + managed = False + db_table = "schema_tag" + + class UniqueTest(models.Model): year = models.IntegerField() slug = models.SlugField(unique=False) diff --git a/tests/modeltests/schema/tests.py b/tests/modeltests/schema/tests.py index 7031138280..18f1abad63 100644 --- a/tests/modeltests/schema/tests.py +++ b/tests/modeltests/schema/tests.py @@ -6,7 +6,7 @@ from django.db import connection, DatabaseError, IntegrityError from django.db.models.fields import IntegerField, TextField, CharField, SlugField from django.db.models.fields.related import ManyToManyField from django.db.models.loading import cache -from .models import Author, Book, AuthorWithM2M, Tag, UniqueTest +from .models import Author, Book, AuthorWithM2M, Tag, TagUniqueRename, UniqueTest class SchemaTests(TestCase): @@ -297,6 +297,21 @@ class SchemaTests(TestCase): Tag.objects.create(title="foo", slug="foo") self.assertRaises(IntegrityError, Tag.objects.create, title="bar", slug="foo") connection.rollback() + # Rename the field + new_field = SlugField(unique=False) + new_field.set_attributes_from_name("slug2") + editor = connection.schema_editor() + editor.start() + editor.alter_field( + Tag, + Tag._meta.get_field_by_name("slug")[0], + TagUniqueRename._meta.get_field_by_name("slug2")[0], + ) + editor.commit() + # Ensure the field is still unique + TagUniqueRename.objects.create(title="foo", slug2="foo") + self.assertRaises(IntegrityError, TagUniqueRename.objects.create, title="bar", slug2="foo") + connection.rollback() def test_unique_together(self): """