SQLAlchemy with_variant()方法适配MySQL与MariaDB时出现参数数量错误的问题求助及修复方案
解决 TypeError: Variant.with_variant() takes 3 positional arguments but 4 were given
我帮你梳理清楚这个问题的来龙去脉,咱们一步步解决:
首先,你碰到的这个报错核心原因很明确:在你当前使用的SQLAlchemy版本里,with_variant()方法只允许传入两个位置参数——第一个是适配方言的字段类型,第二个是单个方言名称。你最初的代码里,第二个with_variant()一次性传了mysql.INTEGER(...)、"mysql"、"mariadb"三个参数,自然就触发了参数数量不匹配的报错。
临时修复方案(不升级SQLAlchemy)
如果暂时没法升级依赖,最简单的办法就是把多方言的适配拆成两次with_variant()调用,每次只传一个方言:
class RuiMartinsTable(Base): __tablename__ = 'RuiMartins' hits = Column( Integer().with_variant( postgresql.INTEGER, "postgresql" ).with_variant( mysql.INTEGER(unsigned=True), "mysql" ).with_variant( mysql.INTEGER(unsigned=True), "mariadb" ), unique=False, index=True, nullable=False, comment="bla bla bla" )
这样每个调用都符合方法的参数要求,就能正常运行了。
更省心的长期方案:升级SQLAlchemy
好消息是,这个问题已经被官方修复了!更新到包含该修复的SQLAlchemy版本后,with_variant()就支持一次性传入多个方言名称了,你最初写的代码(同时传"mysql"和"mariadb")就能直接正常运行,不用再拆分调用。
更新说明:该问题已在官方修复中得到解决,无需再手动拆分调用。
内容的提问来源于stack exchange,提问作者Rui Martins
相关产品推荐
相关产品推荐

