1
0
mirror of https://github.com/django/django.git synced 2025-10-24 06:06:09 +00:00

Fix AlterField migrations that are related to a RenameModel migration

This commit is contained in:
Chris Beaven
2014-03-11 15:52:29 +13:00
parent 40f6ca54f8
commit 107c9f5453
2 changed files with 53 additions and 5 deletions

View File

@@ -226,6 +226,47 @@ class OperationTests(MigrationTestBase):
self.assertTableExists("test_dlmo_pony")
self.assertTableNotExists("test_rnmo_horse")
def test_rename_model_with_related(self):
"""
Tests the real-world combo of a RenameModel operation with AlterField
for a related field.
"""
project_state = self.set_up_test_model(
"test_rnmowr", related_model=True)
# Test the state alterations
model_operation = migrations.RenameModel("Pony", "Horse")
new_state = project_state.clone()
model_operation.state_forwards("test_rnmowr", new_state)
self.assertNotIn(("test_rnmowr", "pony"), new_state.models)
self.assertIn(("test_rnmowr", "horse"), new_state.models)
self.assertEqual(
"Pony",
project_state.render().get_model("test_rnmowr", "rider")
._meta.get_field_by_name("pony")[0].rel.to._meta.object_name)
field_operation = migrations.AlterField(
"Rider", "pony", models.ForeignKey("Horse"))
field_operation.state_forwards("test_rnmowr", new_state)
self.assertEqual(
"Horse",
new_state.render().get_model("test_rnmowr", "rider")
._meta.get_field_by_name("pony")[0].rel.to._meta.object_name)
# Test the database alterations
self.assertTableExists("test_rnmowr_pony")
self.assertTableNotExists("test_rnmowr_horse")
with connection.schema_editor() as editor:
model_operation.database_forwards("test_rnmowr", editor, project_state, new_state)
field_operation.database_forwards("test_rnmowr", editor, project_state, new_state)
self.assertTableNotExists("test_rnmowr_pony")
self.assertTableExists("test_rnmowr_horse")
# And test reversal
with connection.schema_editor() as editor:
field_operation.database_backwards("test_rnmowr", editor, new_state, project_state)
model_operation.database_backwards("test_rnmowr", editor, new_state, project_state)
self.assertTableExists("test_rnmowr_pony")
self.assertTableNotExists("test_rnmowr_horse")
def test_add_field(self):
"""
Tests the AddField operation.