MCPcopy
hub / github.com/django/django / test_alter_field_pk_fk

Method test_alter_field_pk_fk

tests/migrations/test_operations.py:2648–2759  ·  view source on GitHub ↗

Tests the AlterField operation on primary keys changes any FKs pointing to it.

(self)

Source from the content-addressed store, hash-verified

2646
2647 @skipUnlessDBFeature("supports_foreign_keys")
2648 def test_alter_field_pk_fk(self):
2649 """
2650 Tests the AlterField operation on primary keys changes any FKs pointing
2651 to it.
2652 """
2653 project_state = self.set_up_test_model("test_alflpkfk", related_model=True)
2654 project_state = self.apply_operations(
2655 "test_alflpkfk",
2656 project_state,
2657 [
2658 migrations.CreateModel(
2659 "Stable",
2660 fields=[
2661 ("ponies", models.ManyToManyField("Pony")),
2662 ],
2663 ),
2664 migrations.AddField(
2665 "Pony",
2666 "stables",
2667 models.ManyToManyField("Stable"),
2668 ),
2669 ],
2670 )
2671 # Test the state alteration
2672 operation = migrations.AlterField(
2673 "Pony", "id", models.FloatField(primary_key=True)
2674 )
2675 new_state = project_state.clone()
2676 operation.state_forwards("test_alflpkfk", new_state)
2677 self.assertIsInstance(
2678 project_state.models["test_alflpkfk", "pony"].fields["id"],
2679 models.AutoField,
2680 )
2681 self.assertIsInstance(
2682 new_state.models["test_alflpkfk", "pony"].fields["id"],
2683 models.FloatField,
2684 )
2685
2686 def assertIdTypeEqualsFkType():
2687 with connection.cursor() as cursor:
2688 id_type, id_null = [
2689 (c.type_code, c.null_ok)
2690 for c in connection.introspection.get_table_description(
2691 cursor, "test_alflpkfk_pony"
2692 )
2693 if c.name == "id"
2694 ][0]
2695 fk_type, fk_null = [
2696 (c.type_code, c.null_ok)
2697 for c in connection.introspection.get_table_description(
2698 cursor, "test_alflpkfk_rider"
2699 )
2700 if c.name == "pony_id"
2701 ][0]
2702 m2m_fk_type, m2m_fk_null = [
2703 (c.type_code, c.null_ok)
2704 for c in connection.introspection.get_table_description(
2705 cursor,

Callers

nothing calls this directly

Calls 8

state_forwardsMethod · 0.95
database_forwardsMethod · 0.95
database_backwardsMethod · 0.95
schema_editorMethod · 0.80
assertFKExistsMethod · 0.80
set_up_test_modelMethod · 0.45
apply_operationsMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected