I have a column:
code = db.Column(db.Integer(unsigned=True,zerofill=True))
And when I change that column from Integer to String like so:
code = db.Column(db.String())
And I run:
python manage.py db migrate
the migration does not detect any changes.
Should it not detect at least a change in the column data type?
Migration looks like this after:
def upgrade():
### commands auto generated by Alembic - please adjust! ###
pass
### end Alembic commands ###
def downgrade():
### commands auto generated by Alembic - please adjust! ###
pass
### end Alembic commands ###
There is not something this extension does, I'm simply invoking Alembic for the migration work.
See my answer to a similar question in StackOverflow: http://stackoverflow.com/a/17246043/904393. There you can see what Alembic supports and does not support when generating migrations. Column type changes are not supported in the default configuration, but can be enabled.
Thanks @miguelgrinberg! That makes sense.
I assume you can just run something like this in your config:
with EnvironmentContext(
compare_type=true,
)
But how would you use it in the context of Flask-Migrate? Doesn't Flask-Migrate add it's own settings to Alembic?
You have to edit function run_migrations_online() in env.py:
def run_migrations_online():
# ...
context.configure(
connection=connection,
target_metadata=target_metadata,
compare_type=True
)
# ...
I added the compare_type parameter to my env.py but it is still not recognizing the change in one of my column data types. Is there something else I need to do? I'm working within a virtual env if that matters.
Have you read the documentation? Type comparison is not always accurate. You can customize it if necessary.
My recommendation, however, is that you edit the migration script by hand. You always need to review migration scripts anyway, so it costs you little to hand edit the type changes.
Alright, guess I need to learn how to do that anyway. Thanks!
I also leave this here for other n00bs that come across this thread:
I was editing the env.py file in the Flask-Migrate package. I needed to edit the env.py in my migrations directory that is created after you run the init command.
Yes, that is correct. The env.py in the Flask-Migrate package is a template that is used to generate the env.py in migrations repositories. If you edit the template that will only be used for new migration repositories that you create.
@davedg629 I add the compare_type args in my env.py, which is created after I ran the init command, but it still did not work. When I was using alembic directly, changing the compare_type arg worked.
You can now add compare_type=True in the Migrate constructor.
One more note for posterity, SQLite does not support changing the type of a column. Period. You will always get syntax errors.
Thanks atomicwrites. So the manual approach is best then i gather.
Well if you're using SQLite you can't change the type of a column wether or not you use alembic. It's a limitation of SQLite (thing like this are where the "lite" comes in). You have to either make a new column or jugle with copying into a new table.
Most helpful comment
You have to edit function
run_migrations_online()in env.py: