Django中常见错误

Django常见错误解决方法

You are trying to add a non-nullable field 'publication_date' to book 

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:

解决方法:

可以在publication_date括号中加(default=’True’) 增加默认值

———————————————————————————————–

pymysql.err.InternalError: (1050, "Table 'app01_authordetail' already exists")

解决方法:

因为错误同步,导致正确的表结构无法同步过去,只能删除旧表再同步,适用于测试数据库。

  1. 删除app/migrations/下除init.py外所有的py文件,打开文件,在文件里面删除。

  2. 登录数据库删除所有导入的表;

  3. 再次同步生成迁移文件 python3 manage.py makemigrations;

  4. 执行迁移文件 python3 manage.py migrate.

——————————————————————————————————

Got an error creating the test database: (1007, "Can't create database 'test_django_db'; database exists")

Type 'yes' if you would like to try deleting the test database 'test_django_db', or 'no' to cancel: 

原因:

Django test数据库自动生成的数据库字符集是latin1,不是utf8,所以当表中有中文会报错

设置test数据库字符集为“utf8”

DATABASES = {

'default': {

    'ENGINE': 'django.db.backends.mysql',

    'NAME': 'django_db',

    'USER': 'root',

    'PASSWORD': 'mysql',

    'HOST': '',

    'PORT': '',

    'TEST': {'CHARSET': 'utf8', },

}

}

———————————————————————————————-

django连接mysql时,如果使用的是python2,则需要下载mysql-python。
如果使用的是python3,则需要安装pymysql驱动。
python3安装驱动:pip install pymysql

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值