This is the chain of events that has and is happening
Currently I am at Day 3
There are plenty of documentations on how to get Flask-migrate running if you start from Day 0. You just call flask db init
, flask db migrate
and flask db upgrade
.
However, for my case, it's a bit different. I ran the commands, and my first version of migration is empty. Then I modified my database schema and generated a new migration. Now my latest migration only has 1 line of migration which is adding the new row to the table.
I realized that none of my migrations have the actual schema to create the database, which is supposed to be the first migration you see if you start flask-migrate on day 1.
If I were to clone my repo from scratch:
flask db migrate
will result in alembic.util.exc.CommandError: Target database is not up to date.
.
flask db upgrade
will result in sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "offer" does not exist
.
What can I do to fix this?
You have two options.
1) If you only want to track database migrations going forward
flask db init
, to create the migration repository.flask db migrate
, to generate a migration. The migration script will only have the new column in it.flask db upgrade
to apply the new migration to your database.At this point your database should have the new column, and you can continue working. Repeat the above steps any time you need to make additional changes.
Note that with this approach, you cannot recreate the entire database from scratch. You have to have a way to initialize the database to the schema you had on Day 1, and then you can apply the migration history to that to upgrade it to your current schema.
2) If you want to keep track of the entire migration history, including the schema on the day you are adding Flask-Migrate to your application.
This is a little bit tricky, but it can be done.
flask db init
, to create the migration repository.flask db migrate
. This will generate a migration that contains the entire schema of your database.flask db stamp head
to mark the database as updated.flask db migrate
again, to generate a second migration. The migration script will only have the new column in it.flask db upgrade
to apply the new migration to your database.Hope this helps!