MySQL DB migration: change of string length

2020-04-16 18:03发布

问题:

I'm using SQLAlchemy + alembic to manage my database. I had a string field which was 10 characters long and later on found out that it has to be 20. So I updated the model definition.

class Foo(db.Model):
    __tablename__ = 'foos'
    id = db.Column(db.Integer, primary_key=True)
    foo_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    name = db.Column(db.String(80))

When I run alembic revision --autogenerate, this was not detected. Now I did read the documentation and suspected that this might not be supported. How do I managed such changes in DB gracefully?

回答1:

You need to enable optional column type checking. See this for notes on what is checked by default

context.configure(
    # ...
    compare_type = True
)