使用继承的模型:您正试图在没有默认值的情况下向 typemod 添加一个不可为 null 的字段 'id';

Model using inheritance: You are trying to add a non-nullable field 'id' to typemod without a default;

我在 models.py 中使用模型继承。这是我的代码:

class Email(models.Model):
    stuff = models.CharField(max_length=40, blank=True,
                             null=True, default="")
class TypeMod(Email):
    pass

当我 运行 makemigrations 时,尽管我已经为电子邮件模型中的所有字段设置了默认值,但我还是收到了以下消息:

You are trying to add a non-nullable field 'id' to typemod without a 
default; we can't do that (the database needs something to populate 
existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows 
with a null value for this column)
 2) Quit, and let me add a default in models.py

我做错了什么?!

好吧,我想通了!对于遇到同样问题的任何人,我正在使用 vag运行t 并且这个项目是 运行 在 vm.So 上的问题是父模型之前不是抽象的,所以 table 是在父模型的数据库中创建的。所以当我切换到抽象时,table 仍然存在。我解决它的方法是 运行 "vagrant distroy" 并重新启动虚拟机。

不一定和vagrant有关。我使用了本地 Linux 机器并得到了相同的消息。我的解决方案是删除现有的迁移。