Make Alembic detect column type changes and change the length of string fields
Auto Detect does not detect column type changes by default. You must turn this on by editing the alembic/env.py file.
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='')
to:
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/env.py file and add 'compare_type=True' at two places:
def run_migrations_offline():
...
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True,
compare_type=True # <--- here
)
...
def run_migrations_online():
...
with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
compare_type=True # <--- here
)
...
Now the column type changes will be detected and properly migrated.
Links / credits
Alembic - migration for String length change
https://eshlox.net/2017/08/06/alembic-migration-for-string-length-change
Auto Generating Migrations
https://alembic.sqlalchemy.org/en/latest/autogenerate.html#what-does-autogenerate-detect-and-what-does-it-not-detect
Read more
Alembic SQLAlchemy
Recent
- Hiding database UUID primary keys of your web application
- Don't Repeat Yourself (DRY) with Jinja2
- SQLAlchemy, PostgreSQL, maximum number of rows per user
- Show the values in SQLAlchemy dynamic filters
- Secure data transfer with Public Key encryption and pyNaCl
- rqlite: a high-availability and distributed SQLite alternative
Most viewed
- Using Python's pyOpenSSL to verify SSL certificates downloaded from a host
- Using UUIDs instead of Integer Autoincrement Primary Keys with SQLAlchemy and MariaDb
- Connect to a service on a Docker host from a Docker container
- Using PyInstaller and Cython to create a Python executable
- SQLAlchemy: Using Cascade Deletes to delete related objects
- Flask RESTful API request parameter validation with Marshmallow schemas