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下的源码,代替其执行

至此 结束

相关推荐
笑远12 分钟前
不同服务器架构(x86、ARM、Power、SPARC)对数据库使用的影响
运维·服务器·数据库·架构
Gadus_1 小时前
MySQL事务
数据库·mysql
武帝为此1 小时前
【MySQL 删除数据详解】
android·数据库·mysql
一只小爪子1 小时前
SQL 语句说明
运维·服务器·数据库·sql·mysql
hezf1 小时前
初识 Prisma-结合NestJS
数据库·后端·nestjs
Elastic 中国社区官方博客2 小时前
Elasticsearch:使用稀疏向量提升相关性
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
老马啸西风2 小时前
Neo4j GDS-08-neo4j GDS 库中路径搜索算法介绍
网络·数据库·算法·云原生·中间件·neo4j·
老马啸西风2 小时前
Neo4j GDS-13-neo4j GDS 库中节点插入算法实现
数据库·算法·云原生·中间件·embedding·neo4j
想要打 Acm 的小周同学呀2 小时前
Redis的过期和内存淘汰策略
数据库·redis·缓存
a187927218312 小时前
MySQL Slow Log
数据库·mysql·slow log·慢日志·慢日志解析