1
0
mirror of https://github.com/django/django.git synced 2025-10-23 21:59:11 +00:00

Fixed #31826 -- Made AlterField operation a noop when adding db_column.

AlterField operation with adding a db_column is a noop if the column
name is not changed.
This commit is contained in:
Iuri de Silvio
2020-07-25 10:42:43 -03:00
committed by Mariusz Felisiak
parent 3a6fa1d962
commit 632ccffc49
2 changed files with 61 additions and 2 deletions

View File

@@ -1354,6 +1354,59 @@ class OperationTests(OperationTestBase):
self.assertEqual(definition[1], [])
self.assertEqual(sorted(definition[2]), ["field", "model_name", "name"])
def test_alter_field_add_db_column_noop(self):
"""
AlterField operation is a noop when adding only a db_column and the
column name is not changed.
"""
app_label = 'test_afadbn'
project_state = self.set_up_test_model(app_label, related_model=True)
pony_table = '%s_pony' % app_label
new_state = project_state.clone()
operation = migrations.AlterField('Pony', 'weight', models.FloatField(db_column='weight'))
operation.state_forwards(app_label, new_state)
self.assertIsNone(
project_state.models[app_label, 'pony'].fields['weight'].db_column,
)
self.assertEqual(
new_state.models[app_label, 'pony'].fields['weight'].db_column,
'weight',
)
self.assertColumnExists(pony_table, 'weight')
with connection.schema_editor() as editor:
with self.assertNumQueries(0):
operation.database_forwards(app_label, editor, project_state, new_state)
self.assertColumnExists(pony_table, 'weight')
with connection.schema_editor() as editor:
with self.assertNumQueries(0):
operation.database_backwards(app_label, editor, new_state, project_state)
self.assertColumnExists(pony_table, 'weight')
rider_table = '%s_rider' % app_label
new_state = project_state.clone()
operation = migrations.AlterField(
'Rider',
'pony',
models.ForeignKey('Pony', models.CASCADE, db_column='pony_id'),
)
operation.state_forwards(app_label, new_state)
self.assertIsNone(
project_state.models[app_label, 'rider'].fields['pony'].db_column,
)
self.assertIs(
new_state.models[app_label, 'rider'].fields['pony'].db_column,
'pony_id',
)
self.assertColumnExists(rider_table, 'pony_id')
with connection.schema_editor() as editor:
with self.assertNumQueries(0):
operation.database_forwards(app_label, editor, project_state, new_state)
self.assertColumnExists(rider_table, 'pony_id')
with connection.schema_editor() as editor:
with self.assertNumQueries(0):
operation.database_forwards(app_label, editor, new_state, project_state)
self.assertColumnExists(rider_table, 'pony_id')
def test_alter_field_pk(self):
"""
Tests the AlterField operation on primary keys (for things like PostgreSQL's SERIAL weirdness)