#24163 - Tests altering of ForeignKey to OneToOneField
(self)
| 1893 | |
| 1894 | @skipUnlessDBFeature("supports_foreign_keys", "can_introspect_foreign_keys") |
| 1895 | def test_alter_fk_to_o2o(self): |
| 1896 | """ |
| 1897 | #24163 - Tests altering of ForeignKey to OneToOneField |
| 1898 | """ |
| 1899 | # Create the table |
| 1900 | with connection.schema_editor() as editor: |
| 1901 | editor.create_model(Author) |
| 1902 | editor.create_model(Book) |
| 1903 | # Ensure the field is right to begin with |
| 1904 | columns = self.column_classes(Book) |
| 1905 | self.assertEqual( |
| 1906 | columns["author_id"][0], |
| 1907 | connection.features.introspected_field_types["BigIntegerField"], |
| 1908 | ) |
| 1909 | # Ensure the field is not unique |
| 1910 | author = Author.objects.create(name="Joe") |
| 1911 | Book.objects.create( |
| 1912 | author=author, title="Django 1", pub_date=datetime.datetime.now() |
| 1913 | ) |
| 1914 | Book.objects.create( |
| 1915 | author=author, title="Django 2", pub_date=datetime.datetime.now() |
| 1916 | ) |
| 1917 | Book.objects.all().delete() |
| 1918 | self.assertForeignKeyExists(Book, "author_id", "schema_author") |
| 1919 | # Alter the ForeignKey to OneToOneField |
| 1920 | old_field = Book._meta.get_field("author") |
| 1921 | new_field = OneToOneField(Author, CASCADE) |
| 1922 | new_field.set_attributes_from_name("author") |
| 1923 | with connection.schema_editor() as editor: |
| 1924 | editor.alter_field(Book, old_field, new_field, strict=True) |
| 1925 | columns = self.column_classes(BookWithO2O) |
| 1926 | self.assertEqual( |
| 1927 | columns["author_id"][0], |
| 1928 | connection.features.introspected_field_types["BigIntegerField"], |
| 1929 | ) |
| 1930 | # Ensure the field is unique now |
| 1931 | BookWithO2O.objects.create( |
| 1932 | author=author, title="Django 1", pub_date=datetime.datetime.now() |
| 1933 | ) |
| 1934 | with self.assertRaises(IntegrityError): |
| 1935 | BookWithO2O.objects.create( |
| 1936 | author=author, title="Django 2", pub_date=datetime.datetime.now() |
| 1937 | ) |
| 1938 | self.assertForeignKeyExists(BookWithO2O, "author_id", "schema_author") |
| 1939 | |
| 1940 | def test_alter_field_fk_to_o2o(self): |
| 1941 | with connection.schema_editor() as editor: |
nothing calls this directly
no test coverage detected