Python 目标数据库不是最新的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17768940/
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
Target database is not up to date
提问by GangstaGraham
I'd like to make a migration for a Flask app. I am using Alembic.
我想为 Flask 应用程序进行迁移。我正在使用 Alembic。
However, I receive the following error.
但是,我收到以下错误。
Target database is not up to date.
Online, I read that it has something to do with this. http://alembic.zzzcomputing.com/en/latest/cookbook.html#building-an-up-to-date-database-from-scratch
在网上,我读到它与此有关。 http://alembic.zzzcomputing.com/en/latest/cookbook.html#building-an-up-to-date-database-from-scratch
Unfortunately, I don't quite understand how to get the database up to date and where/how I should write the code given in the link. If you have experience with migrations, can you please explain this for me
不幸的是,我不太明白如何更新数据库以及我应该在哪里/如何编写链接中给出的代码。如果您有迁移经验,能否请您为我解释一下
Thanks
谢谢
采纳答案by davidism
After creating a migration, either manually or as --autogenerate, you must apply it with alembic upgrade head. If you used db.create_all()from a shell, you can use alembic stamp headto indicate that the current state of the database represents the application of all migrations.
创建迁移后,手动或作为--autogenerate,您必须使用alembic upgrade head. 如果你db.create_all()从一个 shell 中使用,你可以用alembic stamp head来表示数据库的当前状态代表所有迁移的应用程序。
回答by GangstaGraham
I had to delete some of my migration files for some reason. Not sure why. But that fixed the problem, kind of.
由于某种原因,我不得不删除一些迁移文件。不知道为什么。但这解决了问题,有点。
One issue is that the database ends up getting updated properly, with all the new tables, etc, but the migration files themselves don't show any changes when I use automigrate.
一个问题是数据库最终得到正确更新,包含所有新表等,但是当我使用自动迁移时,迁移文件本身没有显示任何更改。
If someone has a better solution, please let me know, as right now my solution is kind of hacky.
如果有人有更好的解决方案,请告诉我,因为现在我的解决方案有点笨拙。
回答by Patrick Mutuku
To fix this error, delete the latest migration file ( a python file) then try to perform a migration afresh.
要修复此错误,请删除最新的迁移文件(python 文件),然后尝试重新执行迁移。
回答by Rafael Rotiroti
Try to drop all tables before execute the db upgrade command.
在执行 db upgrade 命令之前尝试删除所有表。
回答by LittleLogic
My stuation is like this question, When I execute "./manage.py db migrate -m 'Add relationship'", the error occused like this " alembic.util.exc.CommandError: Target database is not up to date."
我的情况就像这个问题,当我执行“./manage.py db migrate -m '添加关系'”时,错误是这样的“alembic.util.exc.CommandError:目标数据库不是最新的。”
So I checked the status of my migrate:
所以我检查了我的迁移状态:
(venv) ]#./manage.py db heads
d996b44eca57 (head)
(venv) ]#./manage.py db current
INFO [alembic.runtime.migration] Context impl SQLiteImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
715f79abbd75
and found that the heads and the current are different!
并发现磁头和电流不一样!
I fixed it by doing this steps:
我通过执行以下步骤修复了它:
(venv)]#./manage.py db stamp heads
INFO [alembic.runtime.migration] Context impl SQLiteImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.runtime.migration] Running stamp_revision 715f79abbd75 -> d996b44eca57
And now the current is same to the head
现在电流与头部相同
(venv) ]#./manage.py db current
INFO [alembic.runtime.migration] Context impl SQLiteImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
d996b44eca57 (head)
And now I can do the migrate again.
现在我可以再次进行迁移。
回答by fill_J
To solve this, I drop(delete) the tables in migration and run these commands
为了解决这个问题,我删除(删除)迁移中的表并运行这些命令
flask db migrate
and
和
flask db upgrade
回答by Nsagha Kingsly
This Worked For me
这对我有用
$ flask db stamp head
$ flask db migrate
$ flask db upgrade
回答by Serg
I did too run into different heads and I wanted to change one of the fields from string to integer, so first run:
我也遇到了不同的头,我想将其中一个字段从字符串更改为整数,因此首先运行:
$ flask db stamp head # to make the current the same
$ flask db migrate
$ flask db upgrade
It's solved now!
现在已经解决了!
回答by jbasko
This can also happen if you, like myself, have just started a new project and you are using in-memory SQLite database (sqlite:///:memory:). If you apply a migration on such a database, obviously the next time you want to say auto-generate a revision, the database will still be in its original state (empty), so alembic will be complaining that the target database is not up to date. The solution is to switch to a persisted database.
如果您像我一样刚刚开始一个新项目并且使用内存中的 SQLite 数据库 ( sqlite:///:memory:) ,也会发生这种情况。如果你在这样的数据库上应用迁移,显然下次你想说自动生成修订时,数据库仍将处于其原始状态(空),因此 alembic 会抱怨目标数据库达不到日期。解决方案是切换到持久化数据库。
回答by Sergi Ramón
$ flask db stamp head # To set the revision in the database to the head, without performing any migrations. You can change head to the required change you want.
$ flask db migrate # To detect automatically all the changes.
$ flask db upgrade # To apply all the changes.
You can find more info at the documentation https://flask-migrate.readthedocs.io/en/latest/
您可以在文档https://flask-migrate.readthedocs.io/en/latest/ 中找到更多信息

