信仰coding

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

我电脑上面的MySQL是5.5.6,而刚开始安装的django版本是3.多,不支持现在MySQL的版本

解决方案是:

https://yuntianti.com/posts/fix-django3-mysqlclient-import-error/

下面进入正题:
安装Django3后不想折腾mysqlclient那堆库文件,直接装了pymysql替代mysqlclient。还是老办法,__init__.py 中patch一下:

import pymysql
pymysql.install_as_MySQLdb()

启动项目出现以下异常:

raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

看来是Django3对mysqlclient的要求提高了: 1.3.13. 但pymysql的版本没有跟上。
看了下tracelog指向的异常抛出处的代码, 发现如下代码片段:

 

 果然是有个版本判断并raise了异常,而且校验的是Database库的version_info属性。
那pymysql中的version_onfo属性是怎么返回的呢?找到pymysql源码,发现如下片段:

 

 

实际上pymysql版本号是 0.9.3,却明目张胆篡改version_info欺骗Django😂。这样一来就简单了,patch一下这个属性就行了嘛, 修改__init__.py,多插入一行代码:
import pymysql
pymysql.version_info = (1, 3, 13, "final", 0)
pymysql.install_as_MySQLdb()
然后迁移之后会出现下面的错误:

django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table ((1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(6) NOT NULL)' at line 1"))

解决方案是:
https://www.cnblogs.com/yebaofang/p/9863678.html
完美解决
posted on 2020-04-02 23:50  信仰coding  阅读(485)  评论(0编辑  收藏  举报