使用继承的模型:您试图在没有默认值的情况下向typemod添加不可为空的字段“id”;

时间:2017-05-10 14:55:26

标签: python django database inheritance models

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

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

当我运行makemigrations时,虽然我已经为Email模型中的所有字段设置了默认值,但仍会收到以下消息:

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

我做错了什么?!

2 个答案:

答案 0 :(得分:2)

它不一定与流浪汉有关。我使用原生Linux机器并得到相同的消息。我的解决方案是删除现有的迁移。

答案 1 :(得分:1)

好吧我明白了!对于任何有相同问题的人,我使用vagrant并且这个项目在vm上运行。所以问题是父模型之前不是抽象的,因此在数据库中为父模型创建了一个表。所以当我切换到抽象时,桌子仍在那里。我解决它的方式是我运行“vagrant distroy”并重新启动vm。