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

至此 结束

相关推荐
~~李木子~~19 小时前
MySQL 迁移总结报告
数据库·mysql
有梦想的攻城狮20 小时前
通过Lettuce实现PB3格式对象在Redis中的存储与查询
数据库·redis·缓存·pb3
CodeLongBear20 小时前
MySQL索引篇 -- 从数据页的角度看B+树
mysql·面试
桦020 小时前
MySQL【函数】
数据库·mysql
⑩-21 小时前
Redis(1)
数据库·redis·缓存
2301_8035545221 小时前
主从同步配置的步骤
数据库
无敌最俊朗@21 小时前
00-7天攻破sqlite数据库(总览sqlite)
数据库·sqlite
Access开发易登软件21 小时前
Access导出带图表的 HTML 报表:技术实现详解
数据库·后端·html·vba·导出·access
_Minato_1 天前
数据库知识整理——SQL数据定义
数据库·sql·mysql·oracle·database·数据库开发·数据库架构
程序员卷卷狗1 天前
MySQL 四种隔离级别:从脏读到幻读的全过程
数据库·mysql