7

I accidentally deleted my migrations folder. So I ran flask db init, and everything ran smoothly. But when I ran flask db migrate, it gave me this error:

INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
ERROR [root] Error: Can't locate revision identified by '470572fac7a1'

From what I understand, it's looking for my other migration folder which is long gone. How can I solve this?

davidism
  • 121,510
  • 29
  • 395
  • 339
GM Crow
  • 109
  • 1
  • 1
  • 5

5 Answers5

13

Delete that particular record in the table alembic_version that's in your database. There's only one varchar column called version_num, so this expression should work:

delete from alembic_version where version_num='470572fac7a1';

txtsd
  • 141
  • 7
9

Delete the /migrations directory and rename/replicate your applications database. Then start from scratch.

flask db init
flask db migrate
flask db upgrade

Now repopulate your new empty database with data from your backup.

mx0
  • 6,445
  • 12
  • 49
  • 54
Mark Kortink
  • 1,770
  • 4
  • 21
  • 36
  • After deleting the `/migrations` folder, make sure the new `/migrations` folder contains the following files: alembic.ini, env.py, and script.py.mako. – Brendan Jan 12 '21 at 17:17
  • this worked well for me. It generated an entirely new summary migration file. Now I have just have to deal with the different database name. I suppose I could drop the previous database and follow the steps again to reuse the name which I will try. – jerryb Dec 12 '21 at 22:58
7

You must delete alembic_version table in your database.

Connect to your database, and execute:

DROP TABLE alembic_version;

Please check the following answer for more details about this https://stackoverflow.com/a/32356600

Juan Rodriguez
  • 331
  • 4
  • 8
2

Go to versions/ inside migrations/.

If you do not see the folder versions/ or there is not a revision inside the folder versions/, type the following command to create a revision:

flask db revision

You'll get a revision inside the versions/ folder. Open it and replace the revision id with '470572fac7a1':

revision = "470572fac7a1"

Type flask db migrate again.

Should work.

Graciela Carrillo
  • 894
  • 11
  • 17
0

pull last version from github. And then backup your prod db. Then restore your local db. Delete your alembic table (with cascade).

Open your terminal (with venv) write code below:

rm - rf migrations
python manage.py db init

and open migrations folder. Right click on alembic.ini and choose git>rollback>ok.

python manage.py db migrate
python manage.py db upgrade

or

python manage.py db stamp head
python manage.py db migrate
python manage.py db upgrade
Ayse
  • 576
  • 4
  • 13