'Unable to find source of: ERROR [root] Error: Can't locate revision identified by '..'

I am trying to run migrations from command line and keep getting error: ERROR [root] Error: Can't locate revision identified by 'faf3ebfbe667'

As suggested in other posts I deleted my sqlite db and migration folder (several times). I recreated virtual environment as well. I checked all files in my project folder for a reference to the revision and I cant find anything there. The error persists. I tried to drop alembic_version in db but such table does not exists.

I try to run migrations from comand line as below:

set FLASK_APP=my_app/__init__.py
flask db init
flask db migrate
flask db upgrade


Solution 1:[1]

Go to your database and delete the below table: alembic_versions

Solution 2:[2]

It's my late answer, but I hope it will be helpful for you.

You can use this way to resolve the issue.

$ flask db revision --rev-id faf3ebfbe667
$ flask db migrate
$ flask db upgrade

Thank you.

Solution 3:[3]

Delete in your database the table

alembic_version

and create it again using

$ python run.py db stamp heads

Solution 4:[4]

Go to the folder versions inside migrations (Ex: migrations)

  1. 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
  2. If there is already a revision inside the folder open it and replace the revision id by 'faf3ebfbe667' as follows

    • revision = "faf3ebfbe667"
  3. type again the command flask db migrate.

It worked for me.

Solution 5:[5]

You mentioned above that your database URL is sqlite:///site.db. This is a relative path, the file site.db will be created in the current directory, whatever that might be.

Since you said there is an apparent reference to an inexistent database revision, my guess is that you have more than one database file, and the one you deleted is not the one that is being picked up from the current directory of your running application.

I suggest you switch to a absolute path URL to avoid problems. I'm not sure if this is the problem, but even if it isn't you should use an absolute path. See example below:

import os
basedir = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'site.db')

This will make the absolute location of the database file the same location as the script in which this code is included.

Solution 6:[6]

My case was docker, python 3.8, and ubuntu 20.04 lts. The error message was:

ERROR [root] Error: Can't locate revision identified by '3d42f5f6cf4b'

Since I did not have such a version file in migration/versions, so I just destroyed all containers and images and run sudo docker-compose build again, and now it works.

Solution 7:[7]

In case this helps anyone else, I did the following to resolve the error:

  1. Went to migrations/versions and located the most recently created migration file.
  2. Grabbed that migration's revision identifier (should be under the imports, under a comment that reads # revision identifiers, used by Alembic).
  3. In the alembic_version table, I updated the version_num column with the revision identifier from step two.

Solution 8:[8]

Delete all the _pycache_ , migrations folders and the database. Then make sure you set FLASK_APP=app.py on windows or export FLASK_APP=app.py on MAC before running flask db init, then flask db migrate -m "message" and flask db upgrade. You can check this link for more info https://pypi.org/project/Flask-Migrate/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amrit Prasad
Solution 2 Venus713
Solution 3 crbroflovski
Solution 4 Ruli
Solution 5 Miguel Grinberg
Solution 6 Brian Tompsett - 汤莱恩
Solution 7 user15463777
Solution 8 Vicolas