postgresql alembic util 命令错误找不到标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32311366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:03:57  来源:igfitidea点击:

alembic util command error can't find identifier

postgresqlflask-sqlalchemyalembicsqlalchemy-migrate

提问by André Heringer

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 tutorialand I run the command

我正在尝试使用 alembic 来处理我的项目的本地迁移。它第一次工作,但后来我需要删除文件夹并重新启动。(不要问为什么,我只是不得不)我正在关注本教程并运行命令

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?

现在,似乎 alembic 正在寻找不再存在的修订版。反正有没有让alembic忘记那个文件?或者像重新开始比较从 None 到 -> 再次自动生成?

回答by SirKaiserKai

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:

Alembic 将版本历史存储在您的数据库中。因此,它使用存储在数据库中的值来搜索修订。我的个人数据库的版本号存储在表中alembic_version

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

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

提示:SHOW TABLES如果它是基于 SQL 的数据库,请使用该命令查看表。

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.

然后你应该很高兴在 alembic 中进行工作迁移。

回答by Shonin

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.

SirKaiserKai 的解决方案对我不起作用,可能是因为我上次迁移并删除了我应该保留的文件时犯了一些愚蠢的错误。

Instead of dropping the alembic_revisiontable I just updated the value in version_numto match where I knew my DB was at.

而不是删除alembic_revision表,我只是更新了值version_num以匹配我知道我的数据库所在的位置。

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

确保使用与数据库当前状态匹配的文件的迁移 ID

  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
    
  1. 检查缺少的迁移编号

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

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

如果您的数据库处于迁移文件以外的状态,<true state of DB>那么您只会继续出现错误。但是,alembic upgrade head如果这<true state of DB>是一个从您之前中断的位置继续的迁移文件,您可以运行一个 ,这也将运行此状态后的所有迁移。