Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alembic util command error can't find identifier

I'm trying to use alembic to handle local migrations on my project. It worked the first time, but then I needed to delete the folder and restart.(don't ask why, I just had to) I'm following this tutorial and I run the command

python manage.py db init 

And it was ok. But when I try to run

python manage.py db migrate 

I'm getting this error:

alembic.util.CommandError: Can't locate revision identified by '31b8ab83c7d' 

Now, it seems that alembic is looking for a revision that doesn't exists anymore. There is anyway to make alembic forget that file? Or like restart the comparison from None to -> auto-generated again?

like image 231
André Heringer Avatar asked Aug 31 '15 12:08

André Heringer


People also ask

How to solve -alembic can’t locate revision identified by ID_number?

If you’re using Alembic as your migration tool for sqlalchemy project on your python program, you might encountered this problem before -Alembic Can’t locate revision identified by id_number- here’s how to solve it. You might delete your revision file on alembic folder manually. While in your database, the version pointed to that version.

How do I use Alembic commands?

For documentation on using Alembic commands, please see Tutorial. Alembic commands are all represented by functions in the Commands package. They all accept the same style of usage, being sent the Config object as the first argument. Commands can be run programmatically, by first constructing a Config object, as in:

Where does Alembic store the version history?

Alembic stores the version history in your database. Hence it is using the value stored in your database to search for the revision. The version number for my personal database is stored in the table alembic_version:

Can I have multiple Alembic commands in one transaction?

In many cases, and perhaps more often than not, an application will wish to call upon a series of Alembic commands and/or other features. It is usually a good idea to link multiple commands along a single connection and transaction, if feasible.


2 Answers

Alembic stores the version history in your database. Hence it is using the value stored in your database to search for the revision. The version number for my personal database is stored in the table alembic_version:

mysql> SELECT * FROM alembic_version; +-------------+ | version_num | +-------------+ | c8ad125e063 | +-------------+ 1 row in set (0.00 sec) 

Hint: Use the command SHOW TABLES if it's a SQL based database to see the tables.

To solve your problem simply use the command:

DROP TABLE alembic_version; 

Or whatever the name of database version table is. And then you need to re-init the migration folder using the command:

python manage.py db init 

And then creating a new migration:

python manage.py db migrate 

And then you should be good to go with working migrations in alembic.

like image 110
SirKaiserKai Avatar answered Oct 20 '22 07:10

SirKaiserKai


SirKaiserKai's solution didn't work for me, likely because I made some stupid mistake last time I migrated and deleted a file that I should have kept.

Instead of dropping the alembic_revision table I just updated the value in version_num to match where I knew my DB was at.

Make sure you use the migration ID of the file that matches the current state of your database

  1. Check the missing migration number

    psql=> SELECT * FROM alembic_version; +-------------------------+ |      version_num        | +-------------------------+ | <the missing migration> | +-------------------------+ (1 row) 
  2. Update the value

    psql=> UPDATE alembic_version psql->    SET version_num = '<true state of DB>' psql->    WHERE version_num = '<the missing migration>'; UPDATE 1 

If your database is in a state other than the migration file <true state of DB> then you're just going to continue to have errors. However, you could run a alembic upgrade head if the <true state of DB> is a migration file continuing from where you left off previously, and that would run all the migrations post this state as well.

like image 40
Shonin Avatar answered Oct 20 '22 06:10

Shonin