用Flask-SQLAlchemy反映表会引发RuntimeError:应用程序未注册 [英] Reflecting tables with Flask-SQLAlchemy raises RuntimeError: application not registered

查看:127
本文介绍了用Flask-SQLAlchemy反映表会引发RuntimeError:应用程序未注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SQLite数据库以及一个现有的MySQL数据库来管理用户登录.我将MySQL数据库添加到Flask-SQLAlchemy SQLALCHEMY_BINDS配置中.当我尝试反映表格时,出现以下错误:

I have an SQLite database to manage user logins, along with an existing MySQL database. I added the MySQL database to the Flask-SQLAlchemy SQLALCHEMY_BINDS config. When I try to reflect the tables, I get the following error:

RuntimeError: application not registered on db instance and no application bound to current context

如何正确反映表格?

__init__.py:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from config import config

db = SQLAlchemy()

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    db.init_app(app)

    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    return app

manage.py:

from myapp import create_app
from flask.ext.migrate import Migrate, MigrateCommand

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
    manager.run()

models.py:

from . import db, login_manager

db.Model.metadata.reflect(db.engine)

class Calls(db.Model):
    __table__ = db.Model.metadata.tables['Calls2']
    __bind_key__ = 'calls'

    def __repr__(self):
        return self.test1

Traceback (most recent call last):
  File "./manage.py", line 6, in <module>
    from app.models import User, Role, Permission
  File "/home/ilias/Desktop/client-reporting/app/models.py", line 9, in <module>
    db.Model.metadata.reflect(db.engine)
  File "/home/ilias/Desktop/client-reporting/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 780, in engine
    return self.get_engine(self.get_app())
  File "/home/ilias/Desktop/client-reporting/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 809, in get_app
    raise RuntimeError('application not registered on db '
RuntimeError: application not registered on db instance and no application bound to current context

推荐答案

只有在使用应用程序初始化了扩展名之后,您才能对数据库执行操作,直到使用了应用程序工厂功能才可以对数据库进行操作.将reflect调用移到工厂内部,并使用db.reflect,它反映了 all 的所有绑定,而不仅仅是主要绑定.

You can't perform operations on the database until the extension has been initialized with the app, which won't happen until the app factory function is used. Move the reflect call inside the factory, and use db.reflect, which reflects all the binds, not just the main one.

def create_app():
    app = Flask(__name__)
    ...

    db.init_app(app)
    db.reflect(app=app)
    # you could also push an app context instead of passing app
    # with app.app_context():
    #     db.reflect()

    ...
    return app

由于模型是作为视图的一部分导入的(仅在工厂内部导入),因此元数据将在定义模型时包含已反映的表.

Since the models are imported as part of the views, which are only imported inside the factory, the metadata will contain the reflected tables by the time the models are defined.

这篇关于用Flask-SQLAlchemy反映表会引发RuntimeError:应用程序未注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆