Skip to content

Comments

Fix : TypeDecorator rendering to preserve SchemaType name/schema #1795

Closed
khanjan2708 wants to merge 1 commit intosqlalchemy:mainfrom
khanjan2708:issue-1551
Closed

Fix : TypeDecorator rendering to preserve SchemaType name/schema #1795
khanjan2708 wants to merge 1 commit intosqlalchemy:mainfrom
khanjan2708:issue-1551

Conversation

@khanjan2708
Copy link

Fixes Issue : sqlalchemy/sqlalchemy#13140

Description

This pull request fixes Issue sqlalchemy/sqlalchemy#13140, where sqlalchemy.Enum (and other SchemaTypes like Boolean) lost their name and schema properties when augmented with a sqlalchemy.types.TypeDecorator during autogenerated migrations.

The Fix: I have introduced a _type_repr helper in alembic/autogenerate/render.py that specifically detects TypeDecorator instances. If the implementation of the decorator is a SchemaType, Alembic now manually ensures that the name and schema attributes are preserved in the rendered migration string.

Additionally, _user_autogenerate_prefix was updated to use .get() instead of direct dictionary access to prevent KeyError regressions in certain testing environments where the full autogenerate context might not be initialized.

Checklist

This pull request is:

  • A documentation / typographical error fix
    • Good to go, no issue or tests are needed
  • A short code fix
    • please include the issue number, and create an issue if none exists, which
      must include a complete example of the issue. one line code fixes without an
      issue and demonstration will not be accepted.
    • Please include: Fixes: #<issue number> in the commit message
    • please include tests. one line code fixes without tests will not be accepted.
  • A new feature implementation
    • please include the issue number, and create an issue if none exists, which must
      include a complete example of how the feature would look.
    • Please include: Fixes: #<issue number> in the commit message
    • please include tests.

Have a nice day!

Copy link
Member

@zzzeek zzzeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey there

thanks for this. however I listed out the approaches in https://github.com/sqlalchemy/alembic/issues/1551#issuecomment-2420595115 as well as another one in https://github.com/sqlalchemy/alembic/issues/1551#issuecomment-2420603412

this approach seems to use another approach, which is to get the repr string and then tries to guess how to add "name" into it, which I'd rather not do this this way. The core issue is TypeDecorator.repr is broken and item 3 in my comment at https://github.com/sqlalchemy/alembic/issues/1551#issuecomment-2420595115 is the best way to approach this.

that is, we should be changing TypeDecorator.__repr__ first and foremost. if we want to put an interim fix on the alembic side, then it should not use the TypeDecorator.__repr__ output at all and should re-implement from scratch.

most effective would be my item 3 change, which would place this on the SQLAlchemy side. ill propose that now

@zzzeek
Copy link
Member

zzzeek commented Feb 22, 2026

I'm going to fix this on the SQLAlchemy side and we no longer need this fix on the alembic side. any chance you can address the test failures on your other PR at #1793 ? that PR is stalled as it does not pass tests

@khanjan2708
Copy link
Author

I'm going to fix this on the SQLAlchemy side and we no longer need this fix on the alembic side. any chance you can address the test failures on your other PR at #1793 ? that PR is stalled as it does not pass tests

Working on it too

@zzzeek
Copy link
Member

zzzeek commented Feb 24, 2026

hi -

the sqlalchemy fix is merged, closing this

@zzzeek zzzeek closed this Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants