Cannot complete Flask-Migration

后端 未结 8 2138
刺人心
刺人心 2021-02-02 13:27

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

ProgrammingError: (ProgrammingError) relation          


        
8条回答
  •  一向
    一向 (楼主)
    2021-02-02 13:47

    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: 
    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.

提交回复
热议问题