Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change from Integer to String not detected. #24

Closed
playpianolikewoah opened this issue Apr 29, 2014 · 13 comments
Closed

Change from Integer to String not detected. #24

playpianolikewoah opened this issue Apr 29, 2014 · 13 comments
Labels

Comments

@playpianolikewoah
Copy link

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 ###
@miguelgrinberg
Copy link
Owner

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.

@playpianolikewoah
Copy link
Author

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?

@miguelgrinberg
Copy link
Owner

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
                )
    # ...

@davedg629
Copy link

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.

@miguelgrinberg
Copy link
Owner

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.

@davedg629
Copy link

Alright, guess I need to learn how to do that anyway. Thanks!

@davedg629
Copy link

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.

@miguelgrinberg
Copy link
Owner

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.

@unionx
Copy link

unionx commented Jan 30, 2015

@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.

@miguelgrinberg
Copy link
Owner

You can now add compare_type=True in the Migrate constructor.

@atomicwrites
Copy link

One more note for posterity, SQLite does not support changing the type of a column. Period. You will always get syntax errors.

@alsmith808
Copy link

Thanks atomicwrites. So the manual approach is best then i gather.

@atomicwrites
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants