Django 迁移错误:您无法更改 M2M 字段或从 M2M 字段更改,也无法在 M2M 字段上添加或删除 through=

2024-03-07

我正在尝试将 M2M 字段修改为外键字段。命令 validate 显示没有问题,当我运行syncdb 时:

ValueError: Cannot alter field xxx into yyy they are not compatible types (you cannot alter to or from M2M fields, or add or remove through= on M2M fields)

所以我无法进行迁移。

class InstituteStaff(Person):
    user                 = models.OneToOneField(User, blank=True, null=True)
    investigation_area   = models.ManyToManyField(InvestigationArea, blank=True,)
    investigation_group  = models.ManyToManyField(InvestigationGroup, blank=True)
    council_group        = models.ForeignKey(CouncilGroup, null=True, blank=True)
    #profiles            = models.ManyToManyField(Profiles, null = True, blank = True)
    profiles             = models.ForeignKey(Profiles, null = True, blank = True)

有什么建议么?


我偶然发现了这一点,虽然我不太关心我的数据,但我仍然不想删除整个数据库。所以我打开迁移文件并更改了AlterField()命令给一个RemoveField() and an AddField()命令效果很好。我丢失了特定字段的数据,但没有丢失其他数据。

I.e.

migrations.AlterField(
    model_name='player',
    name='teams',
    field=models.ManyToManyField(related_name='players', through='players.TeamPlayer', to='players.Team'),
),

to

migrations.RemoveField(
    model_name='player',
    name='teams',
),
migrations.AddField(
    model_name='player',
    name='teams',
    field=models.ManyToManyField(related_name='players', through='players.TeamPlayer', to='players.Team'),
),
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Django 迁移错误:您无法更改 M2M 字段或从 M2M 字段更改,也无法在 M2M 字段上添加或删除 through= 的相关文章

随机推荐