Cannot complete Flask-Migration

20,119

Solution 1

flask-migrate will create a table named "alembic_version" in your database.
so you should drop this table and delete migrations folder in your project.
and then use $ python app.py db init again...
I think $ python app.py db migrate will work fine.

Solution 2

the first step to do is run this command alembic current you should get an error as mentioned above (the goal is to make sure that this command returns a valid response).

the reason why u're getting this is bc alembic is confused about your current state.. it's assuming that you should be in revision 39408d6b248d but then decides that that revision is invalid.

to investigate this, let's find out which revisions are considered valid by alembic, run this command:

alembic history --verbose

you'll get a list of all previous revisions (note: it's a good idea to attach a message beside each revision.. think about it as a good git commit message)

Rev: 594cc72f56fd (head)
Parent: 262f40e28682
Path: ***************

    adjust context_id in log table so that it is a substring of the object_id

    Revision ID: 594cc72f56fd
    Revises: 262f40e28682
    Create Date: 2015-07-22 14:31:52.424862

Rev: 262f40e28682
Parent: 1dc902bd1c2
Path: ***************

    add context_id column to log table

    Revision ID: 262f40e28682
    Revises: 1dc902bd1c2
    Create Date: 2015-07-22 11:05:37.654553

Rev: 1dc902bd1c2
Parent: <base>
Path: ***************

    Initial database setup

    Revision ID: 1dc902bd1c2
    Revises: 
    Create Date: 2015-07-06 09:55:11.439330

the revision 39408d6b248d clearly doesn't exist in the above revisions. This revision is stored in the alembic_table in the database.. you can verify by going to your dbase and running:

$ select * from alembic_version;
 version_num  
--------------
 57ac999dcaa7

so now you should review the state of your database and see where it fits vis-a-vis the revisions outputted above:

in my case, by poking around my dbase it becomes obvious which revision i'm in right now.. which is that dbase has been setup,, but the other revisions haven't been included yet.

so now i replace the value on the dbase with the one i found from the history command above:

vibereel=> update alembic_version set version_num = '1dc902bd1c2';

and now running alembic current returns

INFO  [alembic.migration] Context impl PostgresqlImpl.
INFO  [alembic.migration] Will assume transactional DDL.
1dc902bd1c2

done.

Solution 3

Alembic keeps the migration history in your database, this is why it still recognises there is another revision there. I keep my project on Heroku so I was able to just do heroku pg:pull ... to be able to get a new copy of my database. Prior to this you will have to drop your local db. In case you don't want to drop your local, I think dropping the table should work too. I use the PG Commander as a GUI tool to quickly browse my databases.

Solution 4

It means that the entry in table alembic_version of your db is "39408d6b248d" and there's no migration file related to it in migrations folder (by default migrations/versions).

So better drop the table alembic_version from your db and do

$ python app.py db history to get the new head of migrations, say, 5301c31377f2

Now run $ python app.py db stamp 5301c31377f2 to let alembic know that it's your migration head (which gets stored in table alembic_version).

Solution 5

Assuming that you have checked that the database exists using psql or pgAdmin, this error usually means exactly what it says. That can be due to either:

Share:
20,119

Related videos on Youtube

Suraj Kapoor
Author by

Suraj Kapoor

Updated on August 24, 2020

Comments

  • Suraj Kapoor
    Suraj Kapoor over 3 years

    I've setup a local Postgres DB with SQLAlchemy and cannot commit my first entry. I keep on getting this error...

    ProgrammingError: (ProgrammingError) relation "user" does not exist
    LINE 1: INSERT INTO "user" (name, email, facebook_id, facebook_token...
    

    It seems like the fields aren't matching to those in the database. I'm trying to migrate using flask-migrate but, when I run $ python app.py db migrate I get this error...

    raise util.CommandError("No such revision '%s'" % id_)
    alembic.util.CommandError: No such revision '39408d6b248d'
    

    It may be best to delete everything and start from scratch as it seems I have botched my database setup and / or migration but I'm not sure how to.

    UPDATE: The database has started working now (I dropped and created it again). However, I'm still getting the same error trying to run migrations and it turns out the "no such revision '39408d6b248d' is referring to a migration from an unrelated project. I re-installed flask-migrate but same error.

    • orange
      orange almost 10 years
      Have you ever found a solution to this problem? I'm having the same issue, but I think in my case, the problem is that I want to insert records in a table that was just created in the changeset. I assume the transaction has to be closed first for the table to be available...
  • Suraj Kapoor
    Suraj Kapoor over 10 years
    The db is working now but still have issues re migrations. details are above.
  • Renier
    Renier about 10 years
    I just updated the version_num value in the database to the revision that I needed it to be. But I would never have guessed to check for the migration history in my db...Thanks :)
  • Thibault Martin
    Thibault Martin over 9 years
    Can you detail what you "needed it to be" was? I'm facing the same problem
  • greybeard
    greybeard over 6 years
    (Welcome to SO!) What does your answer add to Cello Hsueh's?