Search code examples
pythonsqlalchemyorm

SQLAlchemy with_variant() for MySQL and MariaDB


I'm trying to run this example,

enter image description here

but I'm getting this error: TypeError: Variant.with_variant() takes 3 positional arguments but 4 were given

My code:

class RuiMartinsTable(Base):
    __tablename__ = 'RuiMartins'

    hits = Column(
        Integer().with_variant(
            postgresql.INTEGER, "postgresql"
        ).with_variant(
            mysql.INTEGER(unsigned=True), "mysql", "mariadb"
        ),
        unique=False,
        index=True,
        nullable=False,
        comment="bla bla bla"
    )

But if I remove , "mariadb", it works:

My code:

class RuiMartinsTable(Base):
    __tablename__ = 'RuiMartins'

    hits = Column(
        Integer().with_variant(
            postgresql.INTEGER, "postgresql"
        ).with_variant(
            mysql.INTEGER(unsigned=True), "mysql"
        ),
        unique=False,
        index=True,
        nullable=False,
        comment="bla bla bla"
    )

Any recommendation? Thanks


UPDATE: Fixed here: https://github.com/sqlalchemy/sqlalchemy/issues/8408

Thank you :)


Solution

  • What happened is you tried to specify two dialect_name parameters in a with_variant which only accepts one (in SQLAlchemy < 2.0):

    method sqlalchemy.types.TypeEngine.with_variant(type_, dialect_name)

    You need to use one with_variant for each dialect you want to specify.

    NB. In SQLAlchemy 2.0, with_variant accepts one or more dialect_names.

    method sqlalchemy.types.TypeEngine.with_variant(type_: _TypeEngineArgument[Any], *dialect_names: str) → SelfTypeEngine

    Looks like the example https://docs.sqlalchemy.org/en/14/core/type_basics.html#using-uppercase-and-backend-specific-types-for-multiple-backends is wrong for 1.x.

    PS. created github issue#8408 to track this documentation issue.