目标数据库不是最新的

Gan*_*ham 56 python sqlalchemy flask alembic

我想为Flask应用程序进行迁移.我正在使用Alembic.

但是,我收到以下错误.

Target database is not up to date.
Run Code Online (Sandbox Code Playgroud)

在线,我读到它与此有关. http://alembic.zzzcomputing.com/en/latest/cookbook.html#building-an-up-to-date-database-from-scratch

不幸的是,我不太明白如何让数据库保持最新状态以及我应该在哪里/如何编写链接中给出的代码.如果您有迁移经验,可以帮我解释一下

谢谢

小智 90

这对我有用

$ flask db stamp head
$ flask db migrate
$ flask db upgrade
Run Code Online (Sandbox Code Playgroud)


dav*_*ism 71

创建迁移后,无论是手动还是作为--autogenerate,都必须应用它alembic upgrade head.如果您使用db.create_all()shell,则可以alembic stamp head用来指示数据库的当前状态表示所有迁移的应用程序.


小智 25

我的想法是这样的,当我执行“ ./manage.py db migration -m'添加关系'”时,出现的错误是这样的:“ alembic.util.exc.CommandError:目标数据库不是最新的。”

因此,我检查了迁移状态:

(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
Run Code Online (Sandbox Code Playgroud)

并发现磁头和电流不同!

我通过执行以下步骤修复了该问题:

(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
Run Code Online (Sandbox Code Playgroud)

而现在的头脑是一样的

(venv) ]#./manage.py db current
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
d996b44eca57 (head)
Run Code Online (Sandbox Code Playgroud)

现在,我可以再次进行迁移了。


Anu*_*kar 20

这可以通过多种方式解决:

1 要修复此错误,请删除最新的迁移文件(python 文件),然后尝试重新执行迁移。

如果问题仍然存在,请尝试以下命令:

$ 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.
Run Code Online (Sandbox Code Playgroud)

  • 我删除了最新的迁移文件。其作品!谢谢 (2认同)

小智 11

$ 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.
Run Code Online (Sandbox Code Playgroud)

您可以在文档https://flask-migrate.readthedocs.io/en/latest/ 中找到更多信息


Gan*_*ham 8

出于某种原因,我不得不删除一些迁移文件.不知道为什么.但这解决了这个问题.

一个问题是数据库最终会正确更新,包含所有新表等,但是当我使用自动迁移时,迁移文件本身不会显示任何更改.

如果有人有更好的解决方案,请告诉我,因为现在我的解决方案有点hacky.


Pat*_*uku 5

要修复此错误,请删除最新的迁移文件(Python 文件),然后尝试重新执行迁移。


小智 5

我也遇到了不同的问题,我想将其中一个字段从字符串更改为整数,所以首先运行:

$ flask db stamp head # to make the current the same
$ flask db migrate
$ flask db upgrade
Run Code Online (Sandbox Code Playgroud)

现在已经解决了!