Tests altering of the primary key
(self)
| 4505 | editor.add_index(Author, index) |
| 4506 | |
| 4507 | def test_primary_key(self): |
| 4508 | """ |
| 4509 | Tests altering of the primary key |
| 4510 | """ |
| 4511 | # Create the table |
| 4512 | with connection.schema_editor() as editor: |
| 4513 | editor.create_model(Tag) |
| 4514 | # Ensure the table is there and has the right PK |
| 4515 | self.assertEqual(self.get_primary_key(Tag._meta.db_table), "id") |
| 4516 | # Alter to change the PK |
| 4517 | id_field = Tag._meta.get_field("id") |
| 4518 | old_field = Tag._meta.get_field("slug") |
| 4519 | new_field = SlugField(primary_key=True) |
| 4520 | new_field.set_attributes_from_name("slug") |
| 4521 | new_field.model = Tag |
| 4522 | with connection.schema_editor() as editor: |
| 4523 | editor.remove_field(Tag, id_field) |
| 4524 | editor.alter_field(Tag, old_field, new_field) |
| 4525 | # Ensure the PK changed |
| 4526 | self.assertNotIn( |
| 4527 | "id", |
| 4528 | self.get_indexes(Tag._meta.db_table), |
| 4529 | ) |
| 4530 | self.assertEqual(self.get_primary_key(Tag._meta.db_table), "slug") |
| 4531 | |
| 4532 | def test_alter_primary_key_the_same_name(self): |
| 4533 | with connection.schema_editor() as editor: |
nothing calls this directly
no test coverage detected