创建蓝本后使用flask-migrate无法创建迁移脚本的问题

背景:

我是一开始没有使用migrate这个功能,后来学到《Flask web开发》第八章后觉得不能迁移数据库太不方便了,就想回来学习下flask-migrate的使用

问题:

但是这时候我的hello.py已经使用蓝本分离成manage.py了,关于数据库的信息位于models.py里面,若是直接像书上一样
先输入python manage.py db init创建迁移仓库,显示的还正常:

Creating directory C:\Users\kai\PycharmProjects\practice\myflasky\migrations ... done
Creating directory C:\Users\kai\PycharmProjects\practice\myflasky\migrations\versions ... done
Generating C:\Users\kai\PycharmProjects\practice\myflasky\migrations\alembic.ini ... done
Generating C:\Users\kai\PycharmProjects\practice\myflasky\migrations\env.py ... done
Generating C:\Users\kai\PycharmProjects\practice\myflasky\migrations\README ... done
Generating C:\Users\kai\PycharmProjects\practice\myflasky\migrations\script.py.mako ... done
Please edit configuration/connection/logging settings in 'C:\\Users\\kai\\PycharmProjects\\practice\\myflasky\\migrations\\alembic.ini' before proceeding.

但是之后再输入python manage.py db upgrade创建迁移脚本后,就不对了:

(base) C:\Users\kai\PycharmProjects\practice\myflasky>python manage.py db migrate -m "initial migration"
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.env] No changes in schema detected.

缺少
Detected added table 'roles'
Detected added table 'users'

这两行,进入migrations文件夹中的versions发现的确是空的,证明创建脚本失败

求解过程:

先是在网上查了一圈,参考了mrchi和杜志鹏的回答,了解到应该是flask-migrate没有侦测到我的db数据导致的,但是我的manage.py中是有
from app import create_app, db
from app.models import User, Role

def make_shell_context():
    return dict(app=current_app, db=db, User=User, Role=Role, Permission=Permission)

manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command('db', MigrateCommand)

借鉴杜志鹏的评论,尝试cd到app中使用python models.py db migrate -m “initial migration”不过直接报错了。
接着参考mrchi的问题中的描述,我在cmd中输入命令前输python manage.py shell进入交互,再输入

from app import db
from app.models import User, Role

来导入模型,在输入创建脚本指令,不过结果还是一样的。
期间我还在segment上提了问题,但是结果也还不是很满意

最终解决办法

在经历前面的折腾,想到我的代码和操作应该是没有问题的,只可能是一些前提错了,于是我就想到了

INFO  [alembic.env] No changes in schema detected.

这句话,既然说没有变化你就不创建,那我就把你(sqlite)删了,看你还说不说没有变化,于是。。。

(base) C:\Users\kai\PycharmProjects\practice\myflasky>python manage.py db migrate -m "inital migration"
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table 'roles'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_roles_default' on '['default']'
INFO  [alembic.autogenerate.compare] Detected added table 'users'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_users_email' on '['email']'
INFO  [alembic.autogenerate.compare] Detected added index 'ix_users_username' on '['username']'
Generating C:\Users\kai\PycharmProjects\practice\myflasky\migrations\versions\c08a64ca29fc_inital_migration.py ... done

(base) C:\Users\kai\PycharmProjects\practice\myflasky>python manage.py db upgrade
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> c08a64ca29fc, inital migration

成功了!
不过顿时觉得自己像个逗比。。。

你可能感兴趣的:(数据库,python,flask,迁移,migrate)