Flask-Migrate用法(Target database is not up to date原因、No changes in schema detected原因)
2016-12-22 11:25
555 查看
跟随Flask Web开发一本书第五章做Flask-Migrate数据库迁移,遇到了Target database is not up to date报错、No changes in schema detected报错。网上搜了很多帖子,中文的、英文的,最终在评论中找到了原因,原因就是我没明白Flask-Migrate数据库迁移的应用场景。
Flask-Migrate数据库迁移应用场景(针对《Flask Web》第五章的讲解,原文要表达如下意思):
比如要对User表增加phone字段,记录每个人手机号,这时候会用到Flask-Migrate,实现对表结构的更改
比如要对User表增加phone字段,记录每个人手机号,这时候会用到Flask-Migrate,实现对表结构的更改
比如要对User表增加phone字段,记录每个人手机号,这时候会用到Flask-Migrate,实现对表结构的更改
(重要的事情说三遍)
步骤:
1. 用db.create_all()创建了User表,表中有id、username、role_id三个字段
2.考虑到后期会对表字段扩充,先用Flask-Migrate对表进行了初始化“python hello.py db init”,执行后生成了migrations文件夹
3.比如在xx场景中,要求记录用户手机号,这时需要对User表增加phone字段,过程:
(1)更改hello.py的User表模型,增加phone字段
class User(db.Model):
......
id = ......
username=......
role_id =......
# 这是新增的phone字段
phone = db.Column(db.String(64))
(2)创建迁移脚本
$ python hello.py db migrate -m "add column phone"
你可以进migrations/version/XXXXXXadd column phone.py这个文件,看到upgrade函数里有了增加字段的sql语句,说明迁移脚本创建成功了
(3) 更新数据库
$ python hello.py db upgrade
4.查下User表结构,字段被增加进去了,且对原数据没影响
-----------------------------------------------------------------------------------------------------------------------------------
更多请关注:FlyTester,关注技术的测试者
QQ群:456850134
web站:www.flytester.org
微信扫描二维码关注:
Flask-Migrate数据库迁移应用场景(针对《Flask Web》第五章的讲解,原文要表达如下意思):
比如要对User表增加phone字段,记录每个人手机号,这时候会用到Flask-Migrate,实现对表结构的更改
比如要对User表增加phone字段,记录每个人手机号,这时候会用到Flask-Migrate,实现对表结构的更改
比如要对User表增加phone字段,记录每个人手机号,这时候会用到Flask-Migrate,实现对表结构的更改
(重要的事情说三遍)
步骤:
1. 用db.create_all()创建了User表,表中有id、username、role_id三个字段
2.考虑到后期会对表字段扩充,先用Flask-Migrate对表进行了初始化“python hello.py db init”,执行后生成了migrations文件夹
3.比如在xx场景中,要求记录用户手机号,这时需要对User表增加phone字段,过程:
(1)更改hello.py的User表模型,增加phone字段
class User(db.Model):
......
id = ......
username=......
role_id =......
# 这是新增的phone字段
phone = db.Column(db.String(64))
(2)创建迁移脚本
$ python hello.py db migrate -m "add column phone"
你可以进migrations/version/XXXXXXadd column phone.py这个文件,看到upgrade函数里有了增加字段的sql语句,说明迁移脚本创建成功了
(3) 更新数据库
$ python hello.py db upgrade
4.查下User表结构,字段被增加进去了,且对原数据没影响
-----------------------------------------------------------------------------------------------------------------------------------
更多请关注:FlyTester,关注技术的测试者
QQ群:456850134
web站:www.flytester.org
微信扫描二维码关注:
相关文章推荐
- flask数据库迁移问题: INFO [alembic.env] No changes in schema detected.
- Exception in thread "main" java.lang.RuntimeException: Hive metastore database is not initialized. Please use schematool (e.g. ./schematool -initSchema -dbType ...) to create the schema. If needed, do
- Project configuration is not up-to-date with pom.xml. Run project configuration update
- deployment is out of date due to changes in tho underlying project contents
- 【Maven】Project configuration is not up-to-date with pom.xml错误解决方法
- Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix
- Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix.
- 导入mvn或者转成mvn后出现错误:Project configuration is not up-to-date with pom.xml. Run project configuration up
- The database schema is too old to perform this operation in this SharePoint cluster. Please upgrade the database and...
- Project configuration is not up-to-date with pom.xml导入maven工程后,出现如下错误: Description Resource
- Maven Project configuration is not up-to-date with pom.xml异常
- Project configuration is not up-to-date with pom.xml
- Project configuration is not up-to-date with pom.xml
- Error message when you try to save a table in SQL Server 2008: "Saving changes is not permitted"
- Deployment is out of date due to changes in the underlying project contents. You'll need to manually 'Redeploy' the project to u
- Service Broker message delivery is not enabled in this database. Use the ALTER DATABASE statement to
- Project configuration is not up-to-date with pom.xml错误解决方法
- Project configuration is not up-to-date with pom.xml
- Project configuration is not up-to-date with pom.xml
- sql server 2008 R2 Error 9003:The log scan number passed to log scan in database is not valid