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

至此 结束

相关推荐
AwhiteV40 分钟前
利用图数据库高效解决 Text2sql 任务中表结构复杂时占用过多大模型上下文的问题
数据库·人工智能·自然语言处理·oracle·大模型·text2sql
m0_595199851 小时前
Redis(以Django为例,含具体操作步骤)
数据库·redis·缓存
爱尚你19931 小时前
MySQL 三大日志:redo log、undo log、binlog 详解
数据库·mysql
小猿姐2 小时前
KubeBlocks AI:AI时代的云原生数据库运维探索
数据库·人工智能·云原生·kubeblocks
NocoBase4 小时前
10 个开源工具,快速构建数据应用
数据库·低代码·开源
麻辣清汤4 小时前
结合BI多维度异常分析(日期-> 商家/渠道->日期(商家/渠道))
数据库·python·sql·finebi
钢铁男儿5 小时前
Python 正则表达式(正则表达式和Python 语言)
python·mysql·正则表达式
Kan先生6 小时前
对象存储解决方案:MinIO 的架构与代码实战
数据库·python
超级迅猛龙6 小时前
保姆级Debezium抽取SQL Server同步kafka
数据库·hadoop·mysql·sqlserver·kafka·linq·cdc
杨过过儿6 小时前
【Task02】:四步构建简单rag(第一章3节)
android·java·数据库