Search code examples
pythonflask-sqlalchemydatabase-migrationalembic

flask migrate automatically changing database


I'm new to Flask and Flask-Migrate. When running python manage.py db migrate -m "explanation", whatever change I made to models.py gets automatically applied to the database (in this case, adding a column to some table).

I thought changes were only supposed to be applied after running python manage.py db upgrade?

Also, in the above example, the migration file contains two functions: upgrade and downgrade. However, they are both empty... Aren't they supposed to be include the changes I wanted to apply to the database?

I've read the documentation and it seems to contradict what is actually happening. Thanks in advance for the help!


Solution

  • It's not how Flask-Migrate works. The migrate command discover and generate schema changes, writing it to a file with the migration description code, with a upgrade and a downgrade functions.

    The database is changed with the python manage.py db upgrade command.

    I suspect you have something dropping your database and recreating it when you run the migrate command. Something like:

    db.drop_all()
    db.create_all()
    

    It'll recreate the database, probably before Flask-Migrate is able to analyze the schema.