Django migration error :you cannot alter to or from M2M fields, or add or remove through= on M2M fields

后端 未结 7 1654
梦如初夏
梦如初夏 2020-12-12 18:48

I\'m trying to modify a M2M field to a ForeignKey field. The command validate shows me no issues and when I run syncdb :

ValueError: Cannot alter field xxx i         


        
7条回答
  •  渐次进展
    2020-12-12 19:36

    I stumbled upon this and although I didn't care about my data much, I still didn't want to delete the whole DB. So I opened the migration file and changed the AlterField() command to a RemoveField() and an AddField() command that worked well. I lost my data on the specific field, but nothing else.

    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'),
    ),
    

提交回复
热议问题