MCPcopy
hub / github.com/django/django / test_alter_fk

Method test_alter_fk

tests/schema/tests.py:1785–1811  ·  view source on GitHub ↗

Tests altering of FKs

(self)

Source from the content-addressed store, hash-verified

1783
1784 @skipUnlessDBFeature("supports_foreign_keys")
1785 def test_alter_fk(self):
1786 """
1787 Tests altering of FKs
1788 """
1789 # Create the table
1790 with connection.schema_editor() as editor:
1791 editor.create_model(Author)
1792 editor.create_model(Book)
1793 # Ensure the field is right to begin with
1794 columns = self.column_classes(Book)
1795 self.assertEqual(
1796 columns["author_id"][0],
1797 connection.features.introspected_field_types["BigIntegerField"],
1798 )
1799 self.assertForeignKeyExists(Book, "author_id", "schema_author")
1800 # Alter the FK
1801 old_field = Book._meta.get_field("author")
1802 new_field = ForeignKey(Author, CASCADE, editable=False)
1803 new_field.set_attributes_from_name("author")
1804 with connection.schema_editor() as editor:
1805 editor.alter_field(Book, old_field, new_field, strict=True)
1806 columns = self.column_classes(Book)
1807 self.assertEqual(
1808 columns["author_id"][0],
1809 connection.features.introspected_field_types["BigIntegerField"],
1810 )
1811 self.assertForeignKeyExists(Book, "author_id", "schema_author")
1812
1813 @skipUnlessDBFeature("supports_foreign_keys")
1814 def test_alter_to_fk(self):

Callers

nothing calls this directly

Calls 8

column_classesMethod · 0.95
ForeignKeyClass · 0.90
schema_editorMethod · 0.80
create_modelMethod · 0.45
get_fieldMethod · 0.45
alter_fieldMethod · 0.45

Tested by

no test coverage detected