mirror of
https://github.com/django/django.git
synced 2025-08-04 02:48:35 +00:00
Fixed #28305 -- Fixed "Cannot change column 'x': used in a foreign key constraint" crash on MySQL with a sequence of AlterField or RenameField operations.
Regression in 45ded053b1
.
This commit is contained in:
parent
d6859a1489
commit
c3e0adcad8
5 changed files with 113 additions and 8 deletions
|
@ -1322,6 +1322,83 @@ class OperationTests(OperationTestBase):
|
|||
operation.database_backwards("test_alflpkfk", editor, new_state, project_state)
|
||||
assertIdTypeEqualsFkType()
|
||||
|
||||
def test_alter_field_reloads_state_on_fk_target_changes(self):
|
||||
"""
|
||||
If AlterField doesn't reload state appropriately, the second AlterField
|
||||
crashes on MySQL due to not dropping the PonyRider.pony foreign key
|
||||
constraint before modifying the column.
|
||||
"""
|
||||
app_label = 'alter_alter_field_reloads_state_on_fk_target_changes'
|
||||
project_state = self.apply_operations(app_label, ProjectState(), operations=[
|
||||
migrations.CreateModel('Rider', fields=[
|
||||
('id', models.CharField(primary_key=True, max_length=100)),
|
||||
]),
|
||||
migrations.CreateModel('Pony', fields=[
|
||||
('id', models.CharField(primary_key=True, max_length=100)),
|
||||
('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE)),
|
||||
]),
|
||||
migrations.CreateModel('PonyRider', fields=[
|
||||
('id', models.AutoField(primary_key=True)),
|
||||
('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE)),
|
||||
]),
|
||||
])
|
||||
project_state = self.apply_operations(app_label, project_state, operations=[
|
||||
migrations.AlterField('Rider', 'id', models.CharField(primary_key=True, max_length=99)),
|
||||
migrations.AlterField('Pony', 'id', models.CharField(primary_key=True, max_length=99)),
|
||||
])
|
||||
|
||||
def test_alter_field_reloads_state_on_fk_with_to_field_target_changes(self):
|
||||
"""
|
||||
If AlterField doesn't reload state appropriately, the second AlterField
|
||||
crashes on MySQL due to not dropping the PonyRider.pony foreign key
|
||||
constraint before modifying the column.
|
||||
"""
|
||||
app_label = 'alter_alter_field_reloads_state_on_fk_with_to_field_target_changes'
|
||||
project_state = self.apply_operations(app_label, ProjectState(), operations=[
|
||||
migrations.CreateModel('Rider', fields=[
|
||||
('id', models.CharField(primary_key=True, max_length=100)),
|
||||
('slug', models.CharField(unique=True, max_length=100)),
|
||||
]),
|
||||
migrations.CreateModel('Pony', fields=[
|
||||
('id', models.CharField(primary_key=True, max_length=100)),
|
||||
('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE, to_field='slug')),
|
||||
('slug', models.CharField(unique=True, max_length=100)),
|
||||
]),
|
||||
migrations.CreateModel('PonyRider', fields=[
|
||||
('id', models.AutoField(primary_key=True)),
|
||||
('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE, to_field='slug')),
|
||||
]),
|
||||
])
|
||||
project_state = self.apply_operations(app_label, project_state, operations=[
|
||||
migrations.AlterField('Rider', 'slug', models.CharField(unique=True, max_length=99)),
|
||||
migrations.AlterField('Pony', 'slug', models.CharField(unique=True, max_length=99)),
|
||||
])
|
||||
|
||||
def test_rename_field_reloads_state_on_fk_target_changes(self):
|
||||
"""
|
||||
If RenameField doesn't reload state appropriately, the AlterField
|
||||
crashes on MySQL due to not dropping the PonyRider.pony foreign key
|
||||
constraint before modifying the column.
|
||||
"""
|
||||
app_label = 'alter_rename_field_reloads_state_on_fk_target_changes'
|
||||
project_state = self.apply_operations(app_label, ProjectState(), operations=[
|
||||
migrations.CreateModel('Rider', fields=[
|
||||
('id', models.CharField(primary_key=True, max_length=100)),
|
||||
]),
|
||||
migrations.CreateModel('Pony', fields=[
|
||||
('id', models.CharField(primary_key=True, max_length=100)),
|
||||
('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE)),
|
||||
]),
|
||||
migrations.CreateModel('PonyRider', fields=[
|
||||
('id', models.AutoField(primary_key=True)),
|
||||
('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE)),
|
||||
]),
|
||||
])
|
||||
project_state = self.apply_operations(app_label, project_state, operations=[
|
||||
migrations.RenameField('Rider', 'id', 'id2'),
|
||||
migrations.AlterField('Pony', 'id', models.CharField(primary_key=True, max_length=99)),
|
||||
])
|
||||
|
||||
def test_rename_field(self):
|
||||
"""
|
||||
Tests the RenameField operation.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue