|
28 | 28 | CustomManyToManyField, InheritedManyToManyField, MediumBlobField,
|
29 | 29 | )
|
30 | 30 | from .models import (
|
31 |
| - Author, AuthorWithDefaultHeight, AuthorWithEvenLongerName, Book, |
32 |
| - BookForeignObj, BookWeak, BookWithLongName, BookWithO2O, BookWithoutAuthor, |
33 |
| - BookWithSlug, IntegerPK, Node, Note, NoteRename, Tag, TagIndexed, |
34 |
| - TagM2MTest, TagUniqueRename, Thing, UniqueTest, new_apps, |
| 31 | + Author, AuthorWithDefaultHeight, AuthorWithEvenLongerName, |
| 32 | + AuthorWithIndexedName, Book, BookForeignObj, BookWeak, BookWithLongName, |
| 33 | + BookWithO2O, BookWithoutAuthor, BookWithSlug, IntegerPK, Node, Note, |
| 34 | + NoteRename, Tag, TagIndexed, TagM2MTest, TagUniqueRename, Thing, |
| 35 | + UniqueTest, new_apps, |
35 | 36 | )
|
36 | 37 |
|
37 | 38 |
|
@@ -1632,6 +1633,46 @@ def test_add_remove_index(self):
|
1632 | 1633 | editor.remove_index(Author, index)
|
1633 | 1634 | self.assertNotIn('name', self.get_indexes(Author._meta.db_table))
|
1634 | 1635 |
|
| 1636 | + def test_remove_db_index_doesnt_remove_custom_indexes(self): |
| 1637 | + """ |
| 1638 | + Changing db_index to False doesn't remove indexes from Meta.indexes. |
| 1639 | + """ |
| 1640 | + with connection.schema_editor() as editor: |
| 1641 | + editor.create_model(AuthorWithIndexedName) |
| 1642 | + # Ensure the table has its index |
| 1643 | + self.assertIn('name', self.get_indexes(AuthorWithIndexedName._meta.db_table)) |
| 1644 | + |
| 1645 | + # Add the custom index |
| 1646 | + index = Index(fields=['-name'], name='author_name_idx') |
| 1647 | + author_index_name = index.name |
| 1648 | + with connection.schema_editor() as editor: |
| 1649 | + db_index_name = editor._create_index_name( |
| 1650 | + model=AuthorWithIndexedName, |
| 1651 | + column_names=('name',), |
| 1652 | + ) |
| 1653 | + if connection.features.uppercases_column_names: |
| 1654 | + author_index_name = author_index_name.upper() |
| 1655 | + db_index_name = db_index_name.upper() |
| 1656 | + try: |
| 1657 | + AuthorWithIndexedName._meta.indexes = [index] |
| 1658 | + with connection.schema_editor() as editor: |
| 1659 | + editor.add_index(AuthorWithIndexedName, index) |
| 1660 | + old_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table) |
| 1661 | + self.assertIn(author_index_name, old_constraints) |
| 1662 | + self.assertIn(db_index_name, old_constraints) |
| 1663 | + # Change name field to db_index=False |
| 1664 | + old_field = AuthorWithIndexedName._meta.get_field('name') |
| 1665 | + new_field = CharField(max_length=255) |
| 1666 | + new_field.set_attributes_from_name('name') |
| 1667 | + with connection.schema_editor() as editor: |
| 1668 | + editor.alter_field(AuthorWithIndexedName, old_field, new_field, strict=True) |
| 1669 | + new_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table) |
| 1670 | + self.assertNotIn(db_index_name, new_constraints) |
| 1671 | + # The index from Meta.indexes is still in the database. |
| 1672 | + self.assertIn(author_index_name, new_constraints) |
| 1673 | + finally: |
| 1674 | + AuthorWithIndexedName._meta.indexes = [] |
| 1675 | + |
1635 | 1676 | def test_order_index(self):
|
1636 | 1677 | """
|
1637 | 1678 | Indexes defined with ordering (ASC/DESC) defined on column
|
|
0 commit comments