问题复现:
代码语言:javascript复制INFO [alembic.runtime.migration] Context impl SQLiteImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.env] No changes in schema detected.
配置文件:
代码语言:javascript复制#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
#
# Authors: limanman
# 51CTOBG: http://xmdevops.blog.51cto.com/
# Purpose:
#
"""
from __future__ import absolute_import
# 说明: 配置基类
class __Config(object):
# -- flask-pids
PID_FILE = 'logs/xmzoomeye-mtr.pid'
# -- flask-sqlalchemy
SQLALCHEMY_ECHO = True
SQLALCHEMY_RECORD_QUERIES = True
SQLALCHEMY_NATIVE_UNICODE = True
SQLALCHEMY_TRACK_MODIFICATIONS = True
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///app/conf/mtrdata.db'
@staticmethod
def init_app(app):
pass
# 说明: 开发环境
class __DevelopmentConfig(__Config):
pass
# 说明: 预测环境
class __TestingConfig(__Config):
pass
# 说明: 正式环境
class __ProductionConfig(__Config):
pass
config = {
'default': __DevelopmentConfig,
'develop': __DevelopmentConfig,
'testing': __TestingConfig,
'product': __ProductionConfig,
}
问题排查:
1. 此应用为一个网络检测展示程序,为了简化就没有使用任务队列,直接后端跑一个mtr检测,利用协程的方式不影响前端数据获取和展示
2. 框架写好后发现迁移命令python xmzoomeye-mtr db init时发现flask-migrate竟然没有检测到我定义的表....., 这是什么鬼?
3. 后来无意间看到网上的一段代码突然发现...自己没有在任何一个文件中导入过自定义的表....动手尝试~ 竟然成功.... 原来flask-migrate是检测上下文中db.Model的子类来创建表的...
解决方案:
代码语言:javascript复制#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
#
# Authors: limanman
# OsChina: http://xmdevops.blog.51cto.com/
# Purpose:
#
"""
# 说明: 导入公共模块
from app import db as _db
from app import create_app
# 说明: 为数据库检测
from app.models import Area, Addr, Info
from flask_script import Manager, Command
from flask_migrate import Migrate, MigrateCommand
# 说明: 导入其它模块
app = create_app()
manager = Manager(app)
migrate = Migrate(app, _db)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
说明: 既然检测上下文中的db.Model的子类,所以只要在任意正确位置导入即可被检测到,so~ 为了方便我直接在入口文件中添加了~尝试再次初始化/迁移/升级~
再次创建:
代码语言:javascript复制D:XmDevOps_Pytestxmzoomeye-mtr>python xmzoomeye-mtr db init
Creating directory D:XmDevOps_Pytestxmzoomeye-mtrmigrations ... done
Creating directory D:XmDevOps_Pytestxmzoomeye-mtrmigrationsversions ... don
e
Generating D:XmDevOps_Pytestxmzoomeye-mtrmigrationsalembic.ini ... done
Generating D:XmDevOps_Pytestxmzoomeye-mtrmigrationsenv.py ... done
Generating D:XmDevOps_Pytestxmzoomeye-mtrmigrationsenv.pyc ... done
Generating D:XmDevOps_Pytestxmzoomeye-mtrmigrationsREADME ... done
Generating D:XmDevOps_Pytestxmzoomeye-mtrmigrationsscript.py.mako ... done
Please edit configuration/connection/logging settings in 'D:\XmDevOps_Py\test
xmzoomeye-mtr\migrations\alembic.ini' before proceeding.
D:XmDevOps_Pytestxmzoomeye-mtr>python xmzoomeye-mtr db migrate
INFO [alembic.runtime.migration] Context impl SQLiteImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.autogenerate.compare] Detected added table 'areas'
INFO [alembic.autogenerate.compare] Detected added index 'ix_areas_areaname' on
'['areaname']'
INFO [alembic.autogenerate.compare] Detected added table 'addrs'
INFO [alembic.autogenerate.compare] Detected added index 'ix_addrs_addr' on '['
addr']'
INFO [alembic.autogenerate.compare] Detected added table 'infos'
Generating D:XmDevOps_Pytestxmzoomeye-mtrmigrationsversionse5295ab2586d_.p
y ... done
D:XmDevOps_Pytestxmzoomeye-mtr>python xmzoomeye-mtr db upgrade
INFO [alembic.runtime.migration] Context impl SQLiteImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.runtime.migration] Running upgrade -> e5295ab2586d, empty messag
e