我已从模型中删除了一个外键字段并添加了另一个字段,因此来自:
class MyModel(models.Model):
fk_a = models.ForeignKey(ModelA)
到
class MyModel(models.Model):
fk_b = models.ForeignKey(ModelB)
我自动生成了迁移,其中包括:
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',
),
如果我打开MySQL并描述该表,那么在应用迁移之前,它看起来像是:
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 | |
+---------------+---------+------+-----+---------+----------------+
当我运行迁移时,出现以下错误:
raise django.core.exceptions.FieldDoesNotExist:
MyModel has no field named u'fk_a'
到底是怎么回事?我已重置,仔细检查迁移和数据库状态,重新生成并重新运行迁移,并发生同样的错误。