Tests creation/altering of indexes
(self)
| 4082 | editor.remove_index(Author, index) |
| 4083 | |
| 4084 | def test_indexes(self): |
| 4085 | """ |
| 4086 | Tests creation/altering of indexes |
| 4087 | """ |
| 4088 | # Create the table |
| 4089 | with connection.schema_editor() as editor: |
| 4090 | editor.create_model(Author) |
| 4091 | editor.create_model(Book) |
| 4092 | # Ensure the table is there and has the right index |
| 4093 | self.assertIn( |
| 4094 | "title", |
| 4095 | self.get_indexes(Book._meta.db_table), |
| 4096 | ) |
| 4097 | # Alter to remove the index |
| 4098 | old_field = Book._meta.get_field("title") |
| 4099 | new_field = CharField(max_length=100, db_index=False) |
| 4100 | new_field.set_attributes_from_name("title") |
| 4101 | with connection.schema_editor() as editor: |
| 4102 | editor.alter_field(Book, old_field, new_field, strict=True) |
| 4103 | # Ensure the table is there and has no index |
| 4104 | self.assertNotIn( |
| 4105 | "title", |
| 4106 | self.get_indexes(Book._meta.db_table), |
| 4107 | ) |
| 4108 | # Alter to re-add the index |
| 4109 | new_field2 = Book._meta.get_field("title") |
| 4110 | with connection.schema_editor() as editor: |
| 4111 | editor.alter_field(Book, new_field, new_field2, strict=True) |
| 4112 | # Ensure the table is there and has the index again |
| 4113 | self.assertIn( |
| 4114 | "title", |
| 4115 | self.get_indexes(Book._meta.db_table), |
| 4116 | ) |
| 4117 | # Add a unique column, verify that creates an implicit index |
| 4118 | new_field3 = BookWithSlug._meta.get_field("slug") |
| 4119 | with connection.schema_editor() as editor: |
| 4120 | editor.add_field(Book, new_field3) |
| 4121 | self.assertIn( |
| 4122 | "slug", |
| 4123 | self.get_uniques(Book._meta.db_table), |
| 4124 | ) |
| 4125 | # Remove the unique, check the index goes with it |
| 4126 | new_field4 = CharField(max_length=20, unique=False) |
| 4127 | new_field4.set_attributes_from_name("slug") |
| 4128 | with connection.schema_editor() as editor: |
| 4129 | editor.alter_field(BookWithSlug, new_field3, new_field4, strict=True) |
| 4130 | self.assertNotIn( |
| 4131 | "slug", |
| 4132 | self.get_uniques(Book._meta.db_table), |
| 4133 | ) |
| 4134 | |
| 4135 | def test_text_field_with_db_index(self): |
| 4136 | with connection.schema_editor() as editor: |
nothing calls this directly
no test coverage detected