[alembic.env] No changes in schema detected?
问题复现: INFO[alembic.runtime.migration]ContextimplSQLiteImpl. INFO[alembic.runtime.migration]Willassumenon-transactionalDDL. INFO[alembic.env]Nochangesinschemadetected. 配置文件: #!/usr/bin/envpython #-*-coding:utf-8-*- """ # #Authors:limanman #51CTOBG:http://xmdevops.blog.51cto.com/ #Purpose: # """ from__future__importabsolute_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 definit_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的子类来创建表的... 解决方案: #!/usr/bin/envpython #-*-coding:utf-8-*- """ # #Authors:limanman #OsChina:http://xmdevops.blog.51cto.com/ #Purpose: # """ #说明:导入公共模块 fromappimportdbas_db fromappimportcreate_app #说明:为数据库检测 fromapp.modelsimportArea,Addr,Info fromflask_scriptimportManager,Command fromflask_migrateimportMigrate,MigrateCommand #说明:导入其它模块 app=create_app() manager=Manager(app) migrate=Migrate(app,_db) manager.add_command('db',MigrateCommand) if__name__=='__main__': manager.run() 说明: 既然检测上下文中的db.Model的子类,所以只要在任意正确位置导入即可被检测到,so~ 为了方便我直接在入口文件中添加了~尝试再次初始化/迁移/升级~ 再次创建: D:\XmDevOps_Py\test\xmzoomeye-mtr>pythonxmzoomeye-mtrdbinit CreatingdirectoryD:\XmDevOps_Py\test\xmzoomeye-mtr\migrations...done CreatingdirectoryD:\XmDevOps_Py\test\xmzoomeye-mtr\migrations\versions...don e GeneratingD:\XmDevOps_Py\test\xmzoomeye-mtr\migrations\alembic.ini...done GeneratingD:\XmDevOps_Py\test\xmzoomeye-mtr\migrations\env.py...done GeneratingD:\XmDevOps_Py\test\xmzoomeye-mtr\migrations\env.pyc...done GeneratingD:\XmDevOps_Py\test\xmzoomeye-mtr\migrations\README...done GeneratingD:\XmDevOps_Py\test\xmzoomeye-mtr\migrations\script.py.mako...done Pleaseeditconfiguration/connection/loggingsettingsin'D:\\XmDevOps_Py\\test\ \xmzoomeye-mtr\\migrations\\alembic.ini'beforeproceeding. D:\XmDevOps_Py\test\xmzoomeye-mtr>pythonxmzoomeye-mtrdbmigrate INFO[alembic.runtime.migration]ContextimplSQLiteImpl. INFO[alembic.runtime.migration]Willassumenon-transactionalDDL. INFO[alembic.autogenerate.compare]Detectedaddedtable'areas' INFO[alembic.autogenerate.compare]Detectedaddedindex'ix_areas_areaname'on '['areaname']' INFO[alembic.autogenerate.compare]Detectedaddedtable'addrs' INFO[alembic.autogenerate.compare]Detectedaddedindex'ix_addrs_addr'on'[' addr']' INFO[alembic.autogenerate.compare]Detectedaddedtable'infos' GeneratingD:\XmDevOps_Py\test\xmzoomeye-mtr\migrations\versions\e5295ab2586d_.p y...done D:\XmDevOps_Py\test\xmzoomeye-mtr>pythonxmzoomeye-mtrdbupgrade INFO[alembic.runtime.migration]ContextimplSQLiteImpl. INFO[alembic.runtime.migration]Willassumenon-transactionalDDL. INFO[alembic.runtime.migration]Runningupgrade->e5295ab2586d,emptymessag e