Target database is not up to date

Alembic

Alembic Problem Overview


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.

Alembic Solutions


Solution 1 - Alembic

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.

Solution 2 - Alembic

This Worked For me

$ flask db stamp head
$ flask db migrate
$ flask db upgrade

Solution 3 - Alembic

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.

Solution 4 - Alembic

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.

Solution 5 - Alembic

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.

Solution 6 - Alembic

$ 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/

Solution 7 - Alembic

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!

Solution 8 - Alembic

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

Solution 9 - Alembic

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.

Solution 10 - Alembic

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

Solution 11 - Alembic

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

flask db migrate

and

flask db upgrade

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGangstaGrahamView Question on Stackoverflow
Solution 1 - AlembicdavidismView Answer on Stackoverflow
Solution 2 - AlembicXeusKingView Answer on Stackoverflow
Solution 3 - AlembicLittleLogicView Answer on Stackoverflow
Solution 4 - AlembicAnupam HaldkarView Answer on Stackoverflow
Solution 5 - AlembicGangstaGrahamView Answer on Stackoverflow
Solution 6 - AlembicSergi RamónView Answer on Stackoverflow
Solution 7 - AlembicSergView Answer on Stackoverflow
Solution 8 - AlembicPatrick MutukuView Answer on Stackoverflow
Solution 9 - AlembicjbaskoView Answer on Stackoverflow
Solution 10 - AlembicRafael RotirotiView Answer on Stackoverflow
Solution 11 - Alembicfill_JView Answer on Stackoverflow