简体   繁体   English

使用Flask-SQLAlchemy反映表会引发RuntimeError:应用程序未注册

[英]Reflecting tables with Flask-SQLAlchemy raises RuntimeError: application not registered

I have an SQLite database to manage user logins, along with an existing MySQL database. 我有一个SQLite数据库来管理用户登录,以及现有的MySQL数据库。 I added the MySQL database to the Flask-SQLAlchemy SQLALCHEMY_BINDS config. 我将MySQL数据库添加到Flask-SQLAlchemy SQLALCHEMY_BINDS配置中。 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

How do I correctly reflect the tables? 我如何正确反映表格?

__init__.py : __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 : 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 : 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

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. 在工厂内移动reflect调用,并使用db.reflect ,它反映所有绑定,而不仅仅是主绑定。

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. 由于模型是作为视图的一部分导入的,这些视图仅在工厂内导入,因此元数据将在定义模型时包含反射表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM