在以下类中添加 description 字段后,
class Colors(models.Model): colors = models.CharField(u'颜色', max_length=10) description = models.CharField(u'描述', max_length=10) def __str__(self): return self.colors
执行以下初始化数据库的步骤,报错
C:PycharmProjectsHelloWorld>python manage.py makemigrations You are trying to add a non-nullable field 'description' to colors without a def ault; 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 Select an option:
这个可能是之前已创建了表中的一条记录,之后模型中增加了一个非空的字段,但是原来已经存在的记录没有这个值
解决方案1:
修改字段为允许为空 null=True
class Colors(models.Model): colors = models.CharField(u'颜色', max_length=10) description = models.CharField(u'描述', max_length=10,null=True) def __str__(self): return self.colors
解决方案2:
先删除整个migrations文件夹,再执行python manage.py makemigrations,和python manage.py migrate命令