改变已经生成迁移文件的数据库表的时候
在改数据库表的时候
要重新生成迁移文件,不然会报错
Your models have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
这个错误就是没重新生成迁移文件
python manage.py makemigrations
运行这个语句的时候会遇到以下的情况
You are trying to add a non-nullable field 'name' to userinfo 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:
这个时候选择1,然后等加载,加载完后继续输入1
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now
Type 'exit' to exit this prompt
>>> 1
这样就ok了
接下来就只要执行迁移文件
python manage.py migrate