在给models.py文件中为已经存在在数据库中的类添加新字段,并进行迁移的时候,可能会出现这个问题:
You are trying to add a non-nullable field 'phone' to addressinfo 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)
2) Quit, and let me add a default in models.py
Select an option:
这是因为该表在数据库中已经有插入的数据,然而新添加的字段(phone)没有设默认值,所以在迁移的时候,会要求你为已有的数据添加新字段(phone)的值。
两个解决方法:
- 加字段的值:添加字段的值:
可以选择退出迁移操作去数据库中修改,也可以选择在当前操作中修改 - 删库
如果修改原有数据嫌麻烦就删库吧,应用下migrations文件里的迁移文件也要删。删完之后,重新建库,迁移就行了。