mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Refs #30172 -- Prevented removing a model Meta's index/unique_together from removing Meta constraints/indexes.
This commit is contained in:
committed by
Tim Graham
parent
4bb859e246
commit
5c17c273ae
@@ -32,10 +32,11 @@ from .fields import (
|
||||
from .models import (
|
||||
Author, AuthorCharFieldWithIndex, AuthorTextFieldWithIndex,
|
||||
AuthorWithDefaultHeight, AuthorWithEvenLongerName, AuthorWithIndexedName,
|
||||
AuthorWithUniqueName, Book, BookForeignObj, BookWeak, BookWithLongName,
|
||||
BookWithO2O, BookWithoutAuthor, BookWithSlug, IntegerPK, Node, Note,
|
||||
NoteRename, Tag, TagIndexed, TagM2MTest, TagUniqueRename, Thing,
|
||||
UniqueTest, new_apps,
|
||||
AuthorWithIndexedNameAndBirthday, AuthorWithUniqueName,
|
||||
AuthorWithUniqueNameAndBirthday, Book, BookForeignObj, BookWeak,
|
||||
BookWithLongName, BookWithO2O, BookWithoutAuthor, BookWithSlug, IntegerPK,
|
||||
Node, Note, NoteRename, Tag, TagIndexed, TagM2MTest, TagUniqueRename,
|
||||
Thing, UniqueTest, new_apps,
|
||||
)
|
||||
|
||||
|
||||
@@ -1839,6 +1840,50 @@ class SchemaTests(TransactionTestCase):
|
||||
with connection.schema_editor() as editor:
|
||||
editor.alter_unique_together(Book, [['author', 'title']], [])
|
||||
|
||||
@skipUnlessDBFeature('allows_multiple_constraints_on_same_fields')
|
||||
def test_remove_unique_together_does_not_remove_meta_constraints(self):
|
||||
with connection.schema_editor() as editor:
|
||||
editor.create_model(AuthorWithUniqueNameAndBirthday)
|
||||
# Add the custom unique constraint
|
||||
constraint = UniqueConstraint(fields=['name', 'birthday'], name='author_name_birthday_uniq')
|
||||
custom_constraint_name = constraint.name
|
||||
AuthorWithUniqueNameAndBirthday._meta.constraints = [constraint]
|
||||
with connection.schema_editor() as editor:
|
||||
editor.add_constraint(AuthorWithUniqueNameAndBirthday, constraint)
|
||||
# Ensure the constraints exist
|
||||
constraints = self.get_constraints(AuthorWithUniqueNameAndBirthday._meta.db_table)
|
||||
self.assertIn(custom_constraint_name, constraints)
|
||||
other_constraints = [
|
||||
name for name, details in constraints.items()
|
||||
if details['columns'] == ['name', 'birthday'] and details['unique'] and name != custom_constraint_name
|
||||
]
|
||||
self.assertEqual(len(other_constraints), 1)
|
||||
# Remove unique together
|
||||
unique_together = AuthorWithUniqueNameAndBirthday._meta.unique_together
|
||||
with connection.schema_editor() as editor:
|
||||
editor.alter_unique_together(AuthorWithUniqueNameAndBirthday, unique_together, [])
|
||||
constraints = self.get_constraints(AuthorWithUniqueNameAndBirthday._meta.db_table)
|
||||
self.assertIn(custom_constraint_name, constraints)
|
||||
other_constraints = [
|
||||
name for name, details in constraints.items()
|
||||
if details['columns'] == ['name', 'birthday'] and details['unique'] and name != custom_constraint_name
|
||||
]
|
||||
self.assertEqual(len(other_constraints), 0)
|
||||
# Re-add unique together
|
||||
with connection.schema_editor() as editor:
|
||||
editor.alter_unique_together(AuthorWithUniqueNameAndBirthday, [], unique_together)
|
||||
constraints = self.get_constraints(AuthorWithUniqueNameAndBirthday._meta.db_table)
|
||||
self.assertIn(custom_constraint_name, constraints)
|
||||
other_constraints = [
|
||||
name for name, details in constraints.items()
|
||||
if details['columns'] == ['name', 'birthday'] and details['unique'] and name != custom_constraint_name
|
||||
]
|
||||
self.assertEqual(len(other_constraints), 1)
|
||||
# Drop the unique constraint
|
||||
with connection.schema_editor() as editor:
|
||||
AuthorWithUniqueNameAndBirthday._meta.constraints = []
|
||||
editor.remove_constraint(AuthorWithUniqueNameAndBirthday, constraint)
|
||||
|
||||
def test_index_together(self):
|
||||
"""
|
||||
Tests removing and adding index_together constraints on a model.
|
||||
@@ -1917,6 +1962,50 @@ class SchemaTests(TransactionTestCase):
|
||||
),
|
||||
)
|
||||
|
||||
@skipUnlessDBFeature('allows_multiple_constraints_on_same_fields')
|
||||
def test_remove_index_together_does_not_remove_meta_indexes(self):
|
||||
with connection.schema_editor() as editor:
|
||||
editor.create_model(AuthorWithIndexedNameAndBirthday)
|
||||
# Add the custom index
|
||||
index = Index(fields=['name', 'birthday'], name='author_name_birthday_idx')
|
||||
custom_index_name = index.name
|
||||
AuthorWithIndexedNameAndBirthday._meta.indexes = [index]
|
||||
with connection.schema_editor() as editor:
|
||||
editor.add_index(AuthorWithIndexedNameAndBirthday, index)
|
||||
# Ensure the indexes exist
|
||||
constraints = self.get_constraints(AuthorWithIndexedNameAndBirthday._meta.db_table)
|
||||
self.assertIn(custom_index_name, constraints)
|
||||
other_constraints = [
|
||||
name for name, details in constraints.items()
|
||||
if details['columns'] == ['name', 'birthday'] and details['index'] and name != custom_index_name
|
||||
]
|
||||
self.assertEqual(len(other_constraints), 1)
|
||||
# Remove index together
|
||||
index_together = AuthorWithIndexedNameAndBirthday._meta.index_together
|
||||
with connection.schema_editor() as editor:
|
||||
editor.alter_index_together(AuthorWithIndexedNameAndBirthday, index_together, [])
|
||||
constraints = self.get_constraints(AuthorWithIndexedNameAndBirthday._meta.db_table)
|
||||
self.assertIn(custom_index_name, constraints)
|
||||
other_constraints = [
|
||||
name for name, details in constraints.items()
|
||||
if details['columns'] == ['name', 'birthday'] and details['index'] and name != custom_index_name
|
||||
]
|
||||
self.assertEqual(len(other_constraints), 0)
|
||||
# Re-add index together
|
||||
with connection.schema_editor() as editor:
|
||||
editor.alter_index_together(AuthorWithIndexedNameAndBirthday, [], index_together)
|
||||
constraints = self.get_constraints(AuthorWithIndexedNameAndBirthday._meta.db_table)
|
||||
self.assertIn(custom_index_name, constraints)
|
||||
other_constraints = [
|
||||
name for name, details in constraints.items()
|
||||
if details['columns'] == ['name', 'birthday'] and details['index'] and name != custom_index_name
|
||||
]
|
||||
self.assertEqual(len(other_constraints), 1)
|
||||
# Drop the index
|
||||
with connection.schema_editor() as editor:
|
||||
AuthorWithIndexedNameAndBirthday._meta.indexes = []
|
||||
editor.remove_index(AuthorWithIndexedNameAndBirthday, index)
|
||||
|
||||
@isolate_apps('schema')
|
||||
def test_db_table(self):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user