angle-up arrow-clockwise arrow-counterclockwise arrow-down-up arrow-left at calendar card-list chat check envelope folder house info-circle pencil people person person-plus phone plus question-circle search tag trash x

Make Alembic detect column type changes and change the length of string fields

9 March 2019 Updated 28 August 2019 by Peter

Auto Detect does not detect column type changes by default. You must turn this on by editing the alembic/ file.

post main image

By default Alembic does not change the length of string fields, I noticed this after changing a string field from:

description = Column(String(150), server_default='')


description = Column(String(250), server_default='')

No changes were made.

It appears that column type change detection is off by default, so you can test it first, and can be turned on by adding 'compare_type=True' to the context.
Edit the alembic/ file and add 'compare_type=True' at two places:

def run_migrations_offline():
        url=url, target_metadata=target_metadata, literal_binds=True,
        compare_type=True # <--- here

def run_migrations_online():
    with connectable.connect() as connection:
            compare_type=True # <--- here

Now the column type changes will be detected and properly migrated.

Links / credits

Alembic - migration for String length change

Auto Generating Migrations

Leave a comment

Comment anonymously or log in to comment.


Leave a reply

Reply anonymously or log in to reply.