Test that unique constraints get ported with column rename

This commit is contained in:
Andrew Godwin 2012-08-18 14:00:42 +01:00
parent f7955c703d
commit 0b01395108
2 changed files with 25 additions and 1 deletions

View File

@ -37,6 +37,15 @@ class Tag(models.Model):
managed = False 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): class UniqueTest(models.Model):
year = models.IntegerField() year = models.IntegerField()
slug = models.SlugField(unique=False) slug = models.SlugField(unique=False)

View File

@ -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 import IntegerField, TextField, CharField, SlugField
from django.db.models.fields.related import ManyToManyField from django.db.models.fields.related import ManyToManyField
from django.db.models.loading import cache 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): class SchemaTests(TestCase):
@ -297,6 +297,21 @@ class SchemaTests(TestCase):
Tag.objects.create(title="foo", slug="foo") Tag.objects.create(title="foo", slug="foo")
self.assertRaises(IntegrityError, Tag.objects.create, title="bar", slug="foo") self.assertRaises(IntegrityError, Tag.objects.create, title="bar", slug="foo")
connection.rollback() 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): def test_unique_together(self):
""" """