有时候在我们使用Django设计了models中的数据库结构,并且已经同步了数据库之后,我们突然想在数据表中更新或者增加新的字段,也就是需要修改数据库的结构,会出现以下的问题:
C:\Users\Administrator\Desktop\Web开发\Django_Demo\jkxy>python manage.py makemigrations
You are trying to add a non-nullable field 'grade' to student without a default; we can't do that (the dat
abase 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:
就是在我们在models中修改了表的字段后,进行python manage.py makemigrations同步数据库时会出现上面报错,会导致数据库结构更新失败
解决方法如下:
第一种方式:先删除再重构
1、删除数据库对应的数据表
注意:在这里可以不用暴力删除数据表,可以利用django的migrations进行,操作如下:
1.1、首先将自己需要重构的数据表类的models注释