Skip to content

fix(sqla): use the wrapped type's converter for TypeDecorator columns - #2976

Open
lprnmns wants to merge 1 commit into
pallets-eco:masterfrom
lprnmns:fix/sqla-typedecorator-converter
Open

lprnmns wants to merge 1 commit into
pallets-eco:masterfrom
lprnmns:fix/sqla-typedecorator-converter

Conversation

@lprnmns

@lprnmns lprnmns commented Oct 15, 2026

Copy link
Copy Markdown

AdminModelConverter.get_converter() looks up a converter using the column type's own class hierarchy. A column with a custom TypeDecorator (the way SQLAlchemy recommends extending built-in types) therefore finds no converter, and the field is silently left out of the create/edit form:

class Timestamp(TypeDecorator):
    impl = DateTime
    cache_ok = True

class Model(db.Model):
    id = Column(Integer, primary_key=True)
    created = Column(Timestamp)   # no "created" field in the form

This adds a fallback to the SQLAlchemy converter: when nothing is registered for the decorator itself, it uses the converter for the wrapped impl type, following nested decorators. Converters registered for a decorator class (e.g. the sqlalchemy_utils types like EmailType or ArrowType) are still found first, so their behaviour doesn't change. Converters that read attributes such as column.type.length keep working because TypeDecorator proxies attribute access to impl.

To reuse the lookup for a type rather than a column, the body of ModelConverterBase.get_converter() moves into a _get_type_converter() helper; get_converter() keeps its signature and behaviour.

fixes #1857

Tests: test_form_type_decorator covers a decorated String(20) (gets a StringField with a Length(max=20) validator), a decorated DateTime and a nested decorator. It fails on master with 'ModelForm' object has no attribute 'name'. The sqla and peewee tests pass. The only other failures need a MongoDB server, plus test_export_tablib, which fails on master as well. ruff and mypy report nothing new. Changelog entry added.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

ModelConverterBase.get_converter does not correctly handle TypeDecorator

1 participant