django2.2.7版本对mysql的支持总是报错 按照下面步骤操作恢复正常使用
发布日期:2021-05-15 04:56:43 浏览次数:24 分类:精选文章

本文共 1453 字,大约阅读时间需要 4 分钟。

# django启动mysql报错

## 关于pymysql驱动的错误
项目目中的__init__.py,写入
```
import pymysql
pymysql.install_as_MySQLdb()
```
## django2.2/mysql ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3
- 进入django安装目录
/opt/conda373/envs/python37/lib/python3.7/site-packages/django/db/backends/mysql
- 找到base.py文件,注释掉 base.py 中如下部分(35/36行)
if version < (1, 3, 3):
     raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)
- AttributeError: ‘str’ object has no attribute ‘decode’
找到operations.py文件(46行,版本不同行数不同哈~自个儿find一下),将decode改为encode
#linux vim 查找快捷键:?decode
if query is not None:
    query = query.decode(errors='replace')
return query
#改为
if query is not None:
    query = query.encode(errors='replace')
return query

## django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')
```
DATABASES= {    
    'default': {        
        'ENGINE': 'django.db.backends.mysql',        
        'NAME': 'ebusiness',        
        'USER':'root',        
        'PASSWORD':'123456',        
        'HOST':'localhost',        
        'PORT':'3306',        
        'OPTIONS': {            
            "init_command": "SET foreign_key_checks = 0;",        
            }, #加入这个在setting.py文件中数据库配置信息中        
        #'OPTIONS':{'init_command':'SET engine=InnoDB',}    
        }
        }

```

## django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency account.0001_initial on database 'default'.

把每个app中的migrations文件下全删除了,重新建库  

python3 manage.py makemigrations appname  
其中python3 manage.py makemigrations 现在不能一次性建好所有应用了,注意app的先后顺序

上一篇:配置NFS固定端口
下一篇:将嵌套 for 循环写成单行

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年05月07日 05时16分09秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章