I have removed one Foreign Key field from a model and added another, so from:
class MyModel(models.Model):
fk_a = models.ForeignKey(ModelA)
to
class MyModel(models.Model):
fk_b = models.ForeignKey(ModelB)
I autogenerated the migration, which includes this:
migrations.AddField(
model_name='mymodel',
name='fk_b',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='myapp.modelb'),
),
migrations.RemoveField(
model_name='mymodel',
name='fk_a',
),
If I open up MySQL and describe the table, before applying the migration, it looks like:
mysql> describe myapp_mymodel;
+---------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| fk_a_id | int(11) | NO | MUL | NULL | |
+---------------+---------+------+-----+---------+----------------+
When I run the migration, I get the following error:
raise django.core.exceptions.FieldDoesNotExist:
MyModel has no field named u'fk_a'
What on earth is going on? I've reset, carefully checked the migration and database state, regenerated and rerun the migration and the same error occurs.