MCPcopy
hub / github.com/django/django / test_primary_key

Method test_primary_key

tests/schema/tests.py:4507–4530  ·  view source on GitHub ↗

Tests altering of the primary key

(self)

Source from the content-addressed store, hash-verified

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:

Callers

nothing calls this directly

Calls 9

get_primary_keyMethod · 0.95
get_indexesMethod · 0.95
SlugFieldClass · 0.90
schema_editorMethod · 0.80
create_modelMethod · 0.45
get_fieldMethod · 0.45
remove_fieldMethod · 0.45
alter_fieldMethod · 0.45

Tested by

no test coverage detected