在Django中,我们操作数据库的时候会遇到这样一个问题:
You are trying to add a non-nullable field 'code' to business 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
这个问题是怎样产生的呢?
首先我们定义一个数据表的类:
class Business(models.Model):
# 默认有个自增的id列,并且是主键
caption = models.CharField(max_length=32)
这是我们输入命令:
python manager.py makemigrations
python manager.py migrate
此时会在数据库中创建一个名为app01_business的数据表(假设我们的app名字是app01),这个表中总共有两列,一列是我们定义的caption列,另一列是Django自动为我们生成的id列,此列是自增的,并且是主键.
接下来我们插入几条数据:
INSERT INTO `cmdb1`.`app01_business` (`id`, `caption`) VALUES ('4', '第四');
UPDATE `cmdb1`.`app01_business` SET `caption`='第一' WHERE `id`='1';
UPDATE `cmdb1`.`app01_business` SET `caption`='第二' WHERE `id`='2';
UPDATE `cmdb1`.`app01_business` SET `caption`='第三' WHERE `id`='3';
假如此时我们有了一个新的需求:在这个表中增加一列code,此时我们就需要在数据表类中来定义:
class Business(models.Model):
# 默认有个自增的id列,并且是主键
caption = models.CharField(max_length=32)
code = models.CharField(max_length=32)
然后执行:
python manager.py makemigrations
此时就会出现:
You are trying to add a non-nullable field 'code' to business 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
出现这个的原因是之前已经创建了这个数据表,并且数据表中已经有数据,如果我们新增加一列的话,Django不知道我们新增加的这一列的值是什么,所有需要我们来处理一下.
处理这个问题的方法有两个:
方法一:
执行第一个选项,然后输入一个默认值,也就是你新增加的这一列的默认值是什么.例如:
You are trying to add a non-nullable field 'code' to business 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
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
>>> 'sa'
Migrations for 'app01':
app01/migrations/0002_business_code.py
- Add field code to business
fml@fml-To-be-filled-by-O-E-M:~/桌面/python学习/django_web/web_fan_1$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, app01, auth, contenttypes, sessions
Running migrations:
Applying app01.0002_business_code... OK
fml@fml-To-be-filled-by-O-E-M:~/桌面/python学习/django_web/web_fan_1$ python manage.py makemigrations
Migrations for 'app01':
app01/migrations/0003_auto_20191014_1024.py
- Alter field code on business
fml@fml-To-be-filled-by-O-E-M:~/桌面/python学习/django_web/web_fan_1$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, app01, auth, contenttypes, sessions
Running migrations:
Applying app01.0003_auto_20191014_1024... OK
我这里把新增加的这一列的默认值设置成了"sa",也就是说每一条数据的"code"列的值都是"sa".
方法二:
在定义数据表类的时候给它一个默认值:
class Business(models.Model):
# 默认有个自增的id列,并且是主键
caption = models.CharField(max_length=32)
code = models.CharField(max_length=32,default='sa')
这样的结果和方法一的结果是一样 .
写在最后
本文是个人的一些学习笔记,如有侵权,请及时联系我进行删除,谢谢大家.