postgresql 无法完成烧瓶迁移

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20973145/
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-10-21 01:15:30  来源:igfitidea点击:

Cannot complete Flask-Migration

pythonpostgresqlsqlalchemy-migrateflask-migrate

提问by Suraj Kapoor

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

我已经使用 SQLAlchemy 设置了本地 Postgres 数据库,但无法提交我的第一个条目。我一直收到这个错误...

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 migrateI get this error...

这些字段似乎与数据库中的字段不匹配。我正在尝试使用 Flask-migrate 进行迁移,但是,当我运行时出现$ python app.py db migrate此错误...

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.

更新:数据库现在已经开始工作(我删除并重新创建了它)。但是,我在尝试运行迁移时仍然遇到相同的错误,结果证明“没有这样的修订版 '39408d6b248d' 指的是从不相关的项目迁移。我重新安装了 Flask-migrate 但同样的错误。

回答by Cello Hsueh

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 initagain...
I think $ python app.py db migratewill work fine.

flask-migrate 将在您的数据库中创建一个名为“alembic_version”的表。
所以你应该删除这个表并删除你项目中的迁移文件夹。
然后$ python app.py db init再次使用...
我认为$ python app.py db migrate会工作得很好。

回答by lawicko

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.

Alembic 将迁移历史记录在您的数据库中,这就是为什么它仍然可以识别出那里有另一个修订版本。我将我的项目保留在 Heroku 上,因此我可以执行heroku pg:pull ...以获得我的数据库的新副本。在此之前,您必须删除本地数据库。如果你不想放弃你的本地人,我认为放弃桌子也应该有效。我使用 PG Commander 作为 GUI 工具来快速浏览我的数据库。

回答by abbood

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

第一步是运行这个命令, alembic current 你应该得到一个如上所述的错误(目标是确保这个命令返回一个有效的响应)。

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 39408d6b248dbut then decides that that revision is invalid.

你得到这个的原因是 bc alembic 对你当前的状态感到困惑..它假设你应该在修订,39408d6b248d但后来决定该修订无效。

to investigate this, let's find out which revisions areconsidered 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)

您将获得所有先前修订的列表(注意:在每个修订旁边附加一条消息是个好主意..将其视为一个好的 git commit 消息)

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 39408d6b248dclearly 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:

上述修订版中39408d6b248d显然不存在该修订版。此修订版存储在数据库的 alembic_table 中。您可以通过转到 dbase 并运行来验证:

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

就我而言,通过查看我的 dbase,很明显我现在处于哪个修订版……即 dbase 已设置,但其他修订版尚未包含在内。

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

所以现在我将 dbase 上的值替换为我从上面的历史命令中找到的值:

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

and now running alembic current returns

现在运行 Alembic 当前回报

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

done.

完毕。

回答by Devi

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

这意味着alembic_version您的数据库表中的条目是“39408d6b248d”,并且迁移文件夹中没有与之相关的迁移文件(默认情况下migrations/versions)。

So better drop the table alembic_versionfrom your db and do

所以最好alembic_version从你的数据库中删除表并做

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

$ python app.py db history获得新的迁移负责人,例如 5301c31377f2

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

现在运行$ python app.py db stamp 5301c31377f2让 alembic 知道它是您的迁移头(它存储在 table 中alembic_version)。

回答by Dmitri Goldring

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:

假设您已经使用 psql 或 pgAdmin 检查了数据库是否存在,这个错误通常意味着它所说的。这可能是由于:

  • 未连接到正确的数据库实例(检查您的数据库网址:主机/端口和数据库名称)
  • 未正确配置 SQLAlchemy(请参阅:SQLAlchemy create_all() 不创建表

回答by byron

I got the same error yesterday, for my case the revision number '39408d6b248d' is due to your previous migration upgrade actions, each time your ran upgrade script then an Alembic version will be generated and stored in data.sqlite.

我昨天遇到了同样的错误,就我而言,修订号“39408d6b248d”是由于您之前的迁移升级操作,每次您运行升级脚本时,都会生成一个 Alembic 版本并将其存储在 data.sqlite 中。

you must have done any upgrade which generated above '39408d6b248d', but then you deleted whole migrations/ directory and removed all upgrade scripts. The database e.g. data.sqlite still stores '39408d6b248d' but there aren't according migration script exists.

您必须已完成在“39408d6b248d”上方生成的任何升级,但随后您删除了整个迁移/目录并删除了所有升级脚本。例如data.sqlite 的数据库仍然存储'39408d6b248d',但不存在相应的迁移脚本。

for my solution I delete whole Alembic versions in database and did all upgrades from scratch.

对于我的解决方案,我删除了数据库中的整个 Alembic 版本,并从头开始进行了所有升级。

回答by matyas

I ran into a similar issue. After performing the python manage.py db migrate command the database tables were not created, there was only an alembic table in the database.

我遇到了类似的问题。执行 python manage.py db migrate 命令后,没有创建数据库表,数据库中只有一个 alembic 表。

I found the solution in the flask-migrate documentation: https://flask-migrate.readthedocs.org/en/latest/

我在烧瓶迁移文档中找到了解决方案:https: //flask-migrate.readthedocs.org/en/latest/

The migration script needs to be reviewed and edited, as Alembic currently does not detect every change you make to your models. In particular, Alembic is currently unable to detect indexes. Once finalized, the migration script also needs to be added to version control.

Then you can apply the migration to the database:

需要检查和编辑迁移脚本,因为 Alembic 当前无法检测您对模型所做的每项更改。特别是,Alembic 目前无法检测索引。完成后,还需要将迁移脚本添加到版本控制中。

然后您可以将迁移应用到数据库:

python manage.py db upgrade

This command created the tables and applied the database migrations.

此命令创建了表并应用了数据库迁移。

回答by mikky

I had the same issue. According to the version in alembic_version table in db, migration action is looking for that version in /migrations/versions folder which has already been deleted. Therefore solution is to delete alembic_version table: If you are using sqlite, 1. Open your xxx.sqlite db file. sqlite3 xxx.sqlite2. check tables .tables3. you will see alembic_version, delete it DROP TABLE alembic_version

我遇到过同样的问题。根据 db 中 alembic_version 表中的版本,迁移操作正在 /migrations/versions 文件夹中查找已删除的版本。因此解决方案是删除 alembic_version 表: 如果您使用的是 sqlite, 1. 打开您的 xxx.sqlite db 文件。 sqlite3 xxx.sqlite2.检查表 .tables3.你会看到alembic_version,删除它 DROP TABLE alembic_version