Tests altering of FKs
(self)
| 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): |
nothing calls this directly
no test coverage detected