Skip to content

Some column types lose variants when inherited [SA 2.0 regression] #11176

@ziima

Description

@ziima

Describe the bug

I discovered that ARRAY type column lose variant when inherited. May affect other column types as well, but not all of them.

In runtime it causes spurios errors when creating model instances. For example my column with ARRAY(Text).with_variant(ScalarListType(), 'sqlite') (ScalarListType from sqlalchemy_utils) ends up with

ProgrammingError: (sqlite3.ProgrammingError) Error binding parameter 3: type 'list' is not supported

Optional link from https://docs.sqlalchemy.org which documents the behavior that is expected

No response

SQLAlchemy Version in Use

2.0.28

DBAPI (i.e. the database driver)

any (tested on sqlite and psycopg2)

Database Vendor and Major Version

sqlite3 3.45.1

Python Version

3.11.8

Operating system

Linux

To Reproduce

from sqlalchemy import ARRAY, Column, Integer, Text
from sqlalchemy.orm import as_declarative, declared_attr


@as_declarative()
class BaseModel:
    id = Column(Integer, primary_key=True)

    @declared_attr
    def __tablename__(cls) -> str:
        return cls.__name__.lower()  # type: ignore


class Direct(BaseModel):
    value = Column(Integer().with_variant(Text(), 'sqlite'))
    array = Column(ARRAY(Text).with_variant(Text(), 'sqlite'))


class Mixin:
    value = Column(Integer().with_variant(Text(), 'sqlite'))
    array = Column(ARRAY(Text).with_variant(Text(), 'sqlite'))


class Indirect(Mixin, BaseModel):
    pass


assert 'sqlite' in Direct.value.type._variant_mapping
assert 'sqlite' in Mixin.value.type._variant_mapping
assert 'sqlite' in Indirect.value.type._variant_mapping
assert 'sqlite' in Direct.array.type._variant_mapping
assert 'sqlite' in Mixin.array.type._variant_mapping
# Fails
assert 'sqlite' in Indirect.array.type._variant_mapping

Error

Traceback (most recent call last):
  File "/tmp/inherit_type.py", line 34, in <module>
    assert 'sqlite' in Indirect.array.type._variant_mapping
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError

Additional context

The model inheritance from the example works correctly under SQLAlchemy 1.4.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdatatypesthings to do with database types, like VARCHAR and othersgreat mcveAn issue with a great mcvenear-term releaseaddition to the milestone which indicates this should be in a near-term releaseorm - annotated declarativeissues with the new annotations-based declarative ORM approach

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions