解决:You are trying to add a non-nullable field 'size' to product without a default

问题描述

使用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

解决方法

法一

  • linux系统

方法一简单粗暴,直接删掉。

将原来的迁移数据库的文件删掉,重新构建。

# 其实这里只需删掉APP文件下0001_initial.py 文件即可
rm  your_app/migrations/*
#此数据库删不删无所谓
rm db.sqlite3
python manage.py makemigrations
python manage.py migrate
  • windows系统
    只能手动删了,然后执行构建数据库的命令
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命令根据脚本呢代码在目标数据库中生成相对于的数据表。

法二

方法二是根据错误提示添加默认值,笔者没有用过,这里只贴上参考方法

参考方法

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值