CommandError: Target database is not up-如何解决

aise util.CommandError("Target database is not up to date.")

CommandError: Target database is not up to date.


http://m.blog.csdn.net/huang5487378/article/details/66973805

数据库迁移Target database is not up to date报错

发表于2017/3/27 17:19:43  201人阅读

分类: flask

《flask web 开发》地第十一章做Flask-Migrate数据库迁移时,遇到了Target database is not up to date报错。

按照书上在模型models.py中增加了一个类Post,并且更新了User类。

表增加字段,实现对表结构的更改, 所以这时候会用到Flask-Migrate来创建数据库迁移脚本(执行 python manage.py db migrate),文件夹migrations\versions会有相应的变化,跟着执行 python manage.py db upgrade 把迁移应用到数据库中,更新数据,且不影响其中保存的数据。一切都如期进行。

后来我发现 模型models.py中表Post的字段avatar_hash拼写错了(写成 avatar_user),所以我在Post表中改正过来,再执行 python manage.py db migrate就报错了 Target database is not up to date。

删除 migrations文件夹再 执行 python manage.py db init,之后再 执行 python manage.py db migrate还是一样报错。

gegoole查到在stackoverflow有人曾经提问过:

Alembic stores the db version in a table it creates called alembic_version. This table contains a single field and row alembic_version.version_num. Make sure the value for this matches the filename of the most recent file in migrations/version. This version number is also contained inside the revision file in the revision variable that generally shows up on line 26 of the file. Make sure it matches the db version.

Another option is to simply drop the db and recreate it using alembic. If this is a development environment, where the data is not important, that would be my recommendation.

大概意思是:

  • Alembic 存储数据库表中它会创建名为alembic_version版本。此表包含一个字段和行alembic_version.version_num。确保这个匹配的值最近的文件的文件名在迁移/版本。这个版本号也包含在修改文件的修改变量,通常出现在文件的第26行。确保它匹配的数据库版本。

  • 另一个选择是使用alembic简单地删除db和重新创建它。如果这是一个开发环境,数据并不重要,这是我的建议。

解决:

我直接删除了flasky文件夹中的.sqlite数据库文件和 migrations文件夹,然后执行 python manage.py db init,之后再 执行 python manage.py db migrate,最后 执行python manage.py db upgrade,完美解决没发现问题。 
只是数据库中已经注册的用户就清空了,需要重新注册,因为只是在学习练习阶段不用担心数据问题,所以删除是最好的办法。

作者:ZivLi
链接:https://www.zhihu.com/question/52720075/answer/193770537
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

看了stackoverflow上的解释我解决了这个问题。它的意思是说你要到migrations/version的目录下,找到你最新的migrate的版本号

<img src="https://i-blog.csdnimg.cn/blog_migrate/5a988415cef6668cc10a706eabfadc23.png" data-rawwidth="583" data-rawheight="186" class="origin_image zh-lightbox-thumb" width="583" data-original="https://pic1.zhimg.com/v2-93d243c71293e0985b768e59272da8a8_r.png">

然后把他修改到你数据库里alembic_version这个表里的version_num的字段上

<img src="https://i-blog.csdnimg.cn/blog_migrate/83f3ea390abd3885c87feea91690c4a2.png" data-rawwidth="244" data-rawheight="153" class="content_image" width="244">

注意没有下划线~,然后再做migrate和upgrade就ok了~希望也能解决你的问题


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值