django.db.utils.NotSupportedError: MySQL 8 or later is required (found 5.7.26).

环境:django 4.11 mysql 5.7 python 3.12.1

时间:20240429

说明:版本不兼容,最初使用注释源码,但是感觉这种处理很低端,所以有了这篇小作文

解决方法一:

1 找到文件:aca\Lib\site-packages\django\db\backends\base\base.py

注释第239行, 即:self.check_database_version_supported()

python 复制代码
def init_connection_state(self):
    """Initialize the database connection settings."""
    global RAN_DB_VERSION_CHECK
    if self.alias not in RAN_DB_VERSION_CHECK:
        # self.check_database_version_supported()
        RAN_DB_VERSION_CHECK.add(self.alias)

该方法验证是否为MySQL 8以上,否则执行raise,也就报错了

2 重载该方法

settings配置

python 复制代码
from django.db.backends.base.base import BaseDatabaseWrapper 


def check_database_version_supported(self):
    """
    在这里修改 check_database_version_supported 方法以适配 MySQL 5.7
    """
    if (
        self.features.minimum_database_version is not None
        and self.get_database_version() < self.features.minimum_database_version
    ):
        db_version = ".".join(map(str, self.get_database_version()))
        min_db_version = ".".join(map(str, self.features.minimum_database_version))
        print(f"WARNNING : The current MySQL version: {db_version}  The recommended MySQL version:{min_db_version}")

BaseDatabaseWrapper.check_database_version_supported = check_database_version_supported

settings文件在程序执行时加载,所以会替换Django下的源码,代替其执行

至此 结束

相关推荐
bbq粉刷匠7 分钟前
MySQL 聚合函数&分组&联合查询
数据库·mysql
赵渝强老师1 小时前
【赵渝强老师】国产金仓数据库的数据库对象
数据库·postgresql·oracle·oceanbase·国产数据库
霖霖总总1 小时前
[小技巧43]MySQL MVCC 深度解析:快照读 vs 当前读
数据库·mysql
使者大牙2 小时前
【单点知识】 Python装饰器介绍
开发语言·数据库·python
数智工坊2 小时前
【操作系统-文件管理】
数据结构·数据库
oioihoii3 小时前
Oracle迁移KingbaseES实战
数据库·oracle
wniuniu_3 小时前
增加依据。。
服务器·网络·数据库
爱敲代码的小鱼3 小时前
事务核心概念与隔离级别解析
java·开发语言·数据库
Mr.徐大人ゞ3 小时前
6.用户及权限管理
数据库·postgresql
赵渝强老师3 小时前
【赵渝强老师】Oracle多租户容器数据库
数据库·oracle