mirror of
https://github.com/django/django.git
synced 2025-08-04 19:08:28 +00:00
Fixed #29245 -- Made autodetector treat field renames + db_column addition as RenameField.
This commit is contained in:
parent
ba5f24ea64
commit
2156565b5b
2 changed files with 70 additions and 2 deletions
|
@ -921,6 +921,67 @@ class AutodetectorTests(TestCase):
|
|||
changes, 'app', 0, 1, model_name='bar', old_name='second', new_name='second_renamed',
|
||||
)
|
||||
|
||||
def test_rename_field_preserved_db_column(self):
|
||||
"""
|
||||
RenameField is used if a field is renamed and db_column equal to the
|
||||
old field's column is added.
|
||||
"""
|
||||
before = [
|
||||
ModelState('app', 'Foo', [
|
||||
('id', models.AutoField(primary_key=True)),
|
||||
('field', models.IntegerField()),
|
||||
]),
|
||||
]
|
||||
after = [
|
||||
ModelState('app', 'Foo', [
|
||||
('id', models.AutoField(primary_key=True)),
|
||||
('renamed_field', models.IntegerField(db_column='field')),
|
||||
]),
|
||||
]
|
||||
changes = self.get_changes(before, after, MigrationQuestioner({'ask_rename': True}))
|
||||
self.assertNumberMigrations(changes, 'app', 1)
|
||||
self.assertOperationTypes(changes, 'app', 0, ['RenameField', 'AlterField'])
|
||||
self.assertOperationAttributes(
|
||||
changes, 'app', 0, 0, model_name='foo', old_name='field', new_name='renamed_field',
|
||||
)
|
||||
self.assertOperationAttributes(changes, 'app', 0, 1, model_name='foo', name='renamed_field')
|
||||
self.assertEqual(changes['app'][0].operations[-1].field.deconstruct(), (
|
||||
'renamed_field', 'django.db.models.IntegerField', [], {'db_column': 'field'},
|
||||
))
|
||||
|
||||
def test_rename_related_field_preserved_db_column(self):
|
||||
before = [
|
||||
ModelState('app', 'Foo', [
|
||||
('id', models.AutoField(primary_key=True)),
|
||||
]),
|
||||
ModelState('app', 'Bar', [
|
||||
('id', models.AutoField(primary_key=True)),
|
||||
('foo', models.ForeignKey('app.Foo', models.CASCADE)),
|
||||
]),
|
||||
]
|
||||
after = [
|
||||
ModelState('app', 'Foo', [
|
||||
('id', models.AutoField(primary_key=True)),
|
||||
]),
|
||||
ModelState('app', 'Bar', [
|
||||
('id', models.AutoField(primary_key=True)),
|
||||
('renamed_foo', models.ForeignKey('app.Foo', models.CASCADE, db_column='foo_id')),
|
||||
]),
|
||||
]
|
||||
changes = self.get_changes(before, after, MigrationQuestioner({'ask_rename': True}))
|
||||
self.assertNumberMigrations(changes, 'app', 1)
|
||||
self.assertOperationTypes(changes, 'app', 0, ['RenameField', 'AlterField'])
|
||||
self.assertOperationAttributes(
|
||||
changes, 'app', 0, 0, model_name='bar', old_name='foo', new_name='renamed_foo',
|
||||
)
|
||||
self.assertOperationAttributes(changes, 'app', 0, 1, model_name='bar', name='renamed_foo')
|
||||
self.assertEqual(changes['app'][0].operations[-1].field.deconstruct(), (
|
||||
'renamed_foo',
|
||||
'django.db.models.ForeignKey',
|
||||
[],
|
||||
{'to': 'app.Foo', 'on_delete': models.CASCADE, 'db_column': 'foo_id'},
|
||||
))
|
||||
|
||||
def test_rename_model(self):
|
||||
"""Tests autodetection of renamed models."""
|
||||
changes = self.get_changes(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue