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")
解决方法:
因为错误同步,导致正确的表结构无法同步过去,只能删除旧表再同步,适用于测试数据库。
删除app/migrations/下除init.py外所有的py文件,打开文件,在文件里面删除。
登录数据库删除所有导入的表;
再次同步生成迁移文件 python3 manage.py makemigrations;
- 执行迁移文件 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