summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Bowman <jbowman@edx.org>2018-04-11 10:19:09 -0400
committerTim Graham <timograham@gmail.com>2018-04-11 23:17:30 -0400
commit95e1191690d2448609b4d0a1eb0b7c822e8a4ac8 (patch)
tree1e2402414522b2e5321e692d3d2bdd6c3f815f72
parentf395b7653dd8d3a94337b2066d696d725b42ca8e (diff)
[2.0.x] Tested altering a unique field when a reverse M2M relation exists.
Backport of 003334f8af29e2023cf7ad7d080aa9ab26a7c528 from master
-rw-r--r--tests/schema/tests.py38
1 files changed, 37 insertions, 1 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index 1fa715da51..cd7e4f638c 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -22,7 +22,7 @@ from django.db.transaction import TransactionManagementError, atomic
from django.test import (
TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature,
)
-from django.test.utils import CaptureQueriesContext, isolate_apps
+from django.test.utils import CaptureQueriesContext, isolate_apps, patch_logger
from django.utils import timezone
from .fields import (
@@ -1538,6 +1538,42 @@ class SchemaTests(TransactionTestCase):
TagUniqueRename.objects.create(title="bar", slug2="foo")
Tag.objects.all().delete()
+ @isolate_apps('schema')
+ @unittest.skipIf(connection.vendor == 'sqlite', 'SQLite remakes the table on field alteration.')
+ def test_unique_and_reverse_m2m(self):
+ """
+ AlterField can modify a unique field when there's a reverse M2M
+ relation on the model.
+ """
+ class Tag(Model):
+ title = CharField(max_length=255)
+ slug = SlugField(unique=True)
+
+ class Meta:
+ app_label = 'schema'
+
+ class Book(Model):
+ tags = ManyToManyField(Tag, related_name='books')
+
+ class Meta:
+ app_label = 'schema'
+
+ with connection.schema_editor() as editor:
+ editor.create_model(Tag)
+ editor.create_model(Book)
+ new_field = SlugField(max_length=75, unique=True)
+ new_field.model = Tag
+ new_field.set_attributes_from_name('slug')
+ with patch_logger('django.db.backends.schema', 'debug') as logger_calls:
+ with connection.schema_editor() as editor:
+ editor.alter_field(Tag, Tag._meta.get_field('slug'), new_field)
+ # One SQL statement is executed to alter the field.
+ self.assertEqual(len(logger_calls), 1)
+ # Ensure that the field is still unique.
+ Tag.objects.create(title='foo', slug='foo')
+ with self.assertRaises(IntegrityError):
+ Tag.objects.create(title='bar', slug='foo')
+
def test_unique_together(self):
"""
Tests removing and adding unique_together constraints on a model.