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

至此 结束

相关推荐
2601_9657984711 分钟前
Salient Theme Setup Guide for Fast Creative WordPress Sites
数据库·web3·php·wordpress
@Mike@1 小时前
09-数据库学习笔记(数据库索引与过滤器)
数据库·笔记
程序员夏洛2 小时前
MySQL 中 count(*)、count(1) 和 count(字段名) 有什么区别?
数据库·mysql
Wang's Blog2 小时前
PostgreSQL笔记34:索引优化策略全景解析——从B-tree到HOT的核心原理与实践
数据库·笔记·postgresql
l1258653 小时前
# RAG向量数据库优化实战:HNSW索引调参与生产级性能设计
数据库·python·mysql·langchain
梦Arrebol3 小时前
Mysql内容及相关实验
数据库·mysql
OceanWaves19933 小时前
mysql 8.0.32 磁盘爆满,清理从库日志
数据库·mysql
凤山老林3 小时前
数据库读写分离与动态路由实战:Spring Boot + ShardingSphere-JDBC 生产配置
数据库·spring boot·后端·分库分表·sharding-jdbc
践行见远4 小时前
django之配置文件加载
django
l1258654 小时前
# RAG噪声知识库治理:一致性与可信度的四层防线设计
数据库·人工智能·python·自然语言处理·langchain