使用Djangon 制作网页,为了方便起见,所用的到的数据库是sqlite3,由于之前在源项目中已构建过表单,于是第二次建数据库时便有了下面的问题;
E:\Users\Administrator\PycharmProjects\MyDjango>python3 manage.py makemigrations
You are trying to add a non-nullable field 'size' to product 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
Select an option: 2
方法一简单粗暴,直接删掉。
将原来的迁移数据库的文件删掉,重新构建。
# 其实这里只需删掉APP文件下0001_initial.py 文件即可
rm your_app/migrations/*
#此数据库删不删无所谓
rm db.sqlite3
python manage.py makemigrations
python manage.py migrate
E:\Users\Administrator\PycharmProjects\MyDjango>python3 manage.py makemigrations
Migrations for 'index':
index\migrations\0001_initial.py
- Create model Product
- Create model Type
- Add field type to product
E:\Users\Administrator\PycharmProjects\MyDjango>python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, index, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
......
makemigrations 命令用于将index所定义的模型生成 0001_initial.py 文件,打开该文件,我们可以看到已将model.py 的内容生成了数据表的脚本代码。而migrate命令根据脚本呢代码在目标数据库中生成相对于的数据表。
方法二是根据错误提示添加默认值,笔者没有用过,这里只贴上参考方法
参考方法