1

这个问题已经在这里讨论和回答,但是这个错误对我来说仍然存在。由于我没有足够的声誉,我还不能在讨论中发表评论。

我已按照响应删除on_delete=models.SET_DEFAULTdefault=1运行初始迁移,但是收到以下错误:

*tutorial_category = models.ForeignKey(TutorialCategory,verbose_name="Category", null=True)
TypeError: __init__() missing 1 required positional argument: 'on_delete'*

我假设它需要 'on_delete' 字段,并且已经尝试过on_delete.PROTECT and CASCADE,但是这会带来原始错误:

*"django.db.utils.IntegrityError: The row in table 'main_tutorial' with primary key '1' has an invalid foreign key: main_tutorial.tutorial_series_id contains a value 'tutorial_series_id' that does not have a corresponding value in main_tutorialseries.id."*

我正在使用 Django 2.2.9,任何有关如何设置外键的帮助将不胜感激。

谢谢!

4

1 回答 1

0

尝试以下任何一种:

tutorial_category = models.ForeignKey(on_delete=django.db.models.deletion.DO_NOTHING,TutorialCategory,verbose_name="Category", null=True)

tutorial_category = models.ForeignKey(on_delete=models.CASCADE,TutorialCategory,verbose_name="Category", null=True)
于 2020-01-24T08:05:47.387 回答