mirror of
https://github.com/django/django.git
synced 2025-08-03 10:34:04 +00:00
Fixed #23630 -- Made AlterModelTable rename auto-created M2M tables.
Thanks Naddiseo for the report, Andrew Godwin for guidance, and Shai Berger for review.
This commit is contained in:
parent
22c85bf1a8
commit
41b337efa0
3 changed files with 46 additions and 2 deletions
|
@ -46,7 +46,9 @@ class OperationTestBase(MigrationTestBase):
|
|||
operation.state_forwards(app_label, new_state)
|
||||
return project_state, new_state
|
||||
|
||||
def set_up_test_model(self, app_label, second_model=False, third_model=False, related_model=False, mti_model=False, proxy_model=False, unique_together=False, options=False):
|
||||
def set_up_test_model(self, app_label, second_model=False, third_model=False,
|
||||
related_model=False, mti_model=False, proxy_model=False,
|
||||
unique_together=False, options=False, db_table=None):
|
||||
"""
|
||||
Creates a test model state and database table.
|
||||
"""
|
||||
|
@ -82,6 +84,8 @@ class OperationTestBase(MigrationTestBase):
|
|||
}
|
||||
if options:
|
||||
model_options["permissions"] = [("can_groom", "Can groom")]
|
||||
if db_table:
|
||||
model_options["db_table"] = db_table
|
||||
operations = [migrations.CreateModel(
|
||||
"Pony",
|
||||
[
|
||||
|
@ -875,6 +879,37 @@ class OperationTests(OperationTestBase):
|
|||
operation.database_backwards("test_almota", editor, new_state, project_state)
|
||||
self.assertTableExists("test_almota_pony")
|
||||
|
||||
def test_alter_model_table_m2m(self):
|
||||
"""
|
||||
AlterModelTable should rename auto-generated M2M tables.
|
||||
"""
|
||||
app_label = "test_talflmltlm2m"
|
||||
pony_db_table = 'pony_foo'
|
||||
project_state = self.set_up_test_model(app_label, second_model=True, db_table=pony_db_table)
|
||||
# Add the M2M field
|
||||
first_state = project_state.clone()
|
||||
operation = migrations.AddField("Pony", "stables", models.ManyToManyField("Stable"))
|
||||
operation.state_forwards(app_label, first_state)
|
||||
with connection.schema_editor() as editor:
|
||||
operation.database_forwards(app_label, editor, project_state, first_state)
|
||||
original_m2m_table = "%s_%s" % (pony_db_table, "stables")
|
||||
new_m2m_table = "%s_%s" % (app_label, "pony_stables")
|
||||
self.assertTableExists(original_m2m_table)
|
||||
self.assertTableNotExists(new_m2m_table)
|
||||
# Rename the Pony db_table which should also rename the m2m table.
|
||||
second_state = first_state.clone()
|
||||
operation = migrations.AlterModelTable(name='pony', table=None)
|
||||
operation.state_forwards(app_label, second_state)
|
||||
with connection.schema_editor() as editor:
|
||||
operation.database_forwards(app_label, editor, first_state, second_state)
|
||||
self.assertTableExists(new_m2m_table)
|
||||
self.assertTableNotExists(original_m2m_table)
|
||||
# And test reversal
|
||||
with connection.schema_editor() as editor:
|
||||
operation.database_backwards(app_label, editor, second_state, first_state)
|
||||
self.assertTableExists(original_m2m_table)
|
||||
self.assertTableNotExists(new_m2m_table)
|
||||
|
||||
def test_alter_field(self):
|
||||
"""
|
||||
Tests the AlterField operation.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue