158

I'd like to make a migration for a Flask app. I am using Alembic.

However, I receive the following error.

Target database is not up to date.

Online, I read that it has something to do with this. http://alembic.zzzcomputing.com/en/latest/cookbook.html#building-an-up-to-date-database-from-scratch

Unfortunately, I don't quite understand how to get the database up to date and where/how I should write the code given in the link.

0

12 Answers 12

173

After creating a migration, either manually or as --autogenerate, you must apply it with alembic upgrade head. If you used db.create_all() from a shell, you can use alembic stamp head to indicate that the current state of the database represents the application of all migrations.

0
147

This Worked For me

$ flask db stamp head
$ flask db migrate
$ flask db upgrade
1
  • Commands for flask only! It's not alembic Incompaitable with other web frameworks
    – Dominux
    Jul 25, 2022 at 9:26
43

My stuation is like this question, When I execute "./manage.py db migrate -m 'Add relationship'", the error occused like this " alembic.util.exc.CommandError: Target database is not up to date."

So I checked the status of my migrate:

(venv) ]#./manage.py db heads
d996b44eca57 (head)
(venv) ]#./manage.py db current
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
715f79abbd75

and found that the heads and the current are different!

I fixed it by doing this steps:

(venv)]#./manage.py db stamp heads
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running stamp_revision 715f79abbd75 -> d996b44eca57

And now the current is same to the head

(venv) ]#./manage.py db current
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
d996b44eca57 (head)

And now I can do the migrate again.

2
  • Works like a charm ! I feel this is the best way to manage this issue ! Dec 23, 2019 at 8:42
  • Same here! Worked smooth. I knew it had do with db heads and current, but didn't know there is "stamp" command. Thanks!
    – Subspacian
    Feb 20, 2020 at 16:47
34

This can be solved bby many ways :

1 To fix this error, delete the latest migration file ( a python file) then try to perform a migration afresh.

If issue still persists try these commands :

$ flask db stamp head  # To set the revision in the database to the head, without performing any migrations. You can change head to the required change you want.
$ flask db migrate     # To detect automatically all the changes.
$ flask db upgrade     # To apply all the changes.
2
  • 1
    I deleted the latest migration file. Its works! thx
    – F.Tamy
    Aug 31, 2021 at 10:08
  • 3 years later and still a gem :) 6 hours ago
13
$ flask db stamp head  # To set the revision in the database to the head, without performing any migrations. You can change head to the required change you want.
$ flask db migrate  # To detect automatically all the changes.
$ flask db upgrade  # To apply all the changes.

You can find more info at the documentation https://flask-migrate.readthedocs.io/en/latest/

11

I had to delete some of my migration files for some reason. Not sure why. But that fixed the problem, kind of.

One issue is that the database ends up getting updated properly, with all the new tables, etc, but the migration files themselves don't show any changes when I use automigrate.

If someone has a better solution, please let me know, as right now my solution is kind of hacky.

1
  • I know it is a bit old now, but do your tables inherit from Base? I had the same issue and automigrate wasn't picking up changes due to the fact that my new tables weren't inheriting from Base where base is Base = declarative_base() and also remember from sqlalchemy.ext.declarative import declarative_base
    – user1709803
    Jul 20, 2015 at 14:08
5

I did too run into different heads and I wanted to change one of the fields from string to integer, so first run:

$ flask db stamp head # to make the current the same
$ flask db migrate
$ flask db upgrade

It's solved now!

4

To fix this error, delete the latest migration file ( a python file) then try to perform a migration afresh.

1
  • This worked for me when I wanted to regenerate a migration file because the first was incomplete. May 18, 2022 at 22:44
1

This can also happen if you, like myself, have just started a new project and you are using in-memory SQLite database (sqlite:///:memory:). If you apply a migration on such a database, obviously the next time you want to say auto-generate a revision, the database will still be in its original state (empty), so alembic will be complaining that the target database is not up to date. The solution is to switch to a persisted database.

1

I also had the same problem input with flask db migrate I used flask db stamp head and then flask db migrate

-3

Try to drop all tables before execute the db upgrade command.

-7

To solve this, I drop(delete) the tables in migration and run these commands

flask db migrate

and

flask db upgrade

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.