当分散烧瓶模型时,RuntimeError:'应用程序未注册到数据库'被引发 [英] When scattering Flask Models, RuntimeError: 'application not registered on db' was raised

查看:349
本文介绍了当分散烧瓶模型时,RuntimeError:'应用程序未注册到数据库'被引发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重新分解我的Flask应用程序,通过分散模型,蓝图,但我有一个运行时错误。

  def create_app():
app = flask.Flask(app)
app.config ['SQLALCHEMY_DATABASE_URI'] ='sqlite://'
app.register_blueprint(api)
db.init_app(app)
db.create_all()
return app

我有以下问题(示例项目位于此处: https://github.com/chfw/sample ):

  Traceback(最近一次调用最后一次):
在< module>文件中的application.py,第17行。
app = create_app()
文件application.py,第12行,在create_app
db.create_all()
文件\AppData\Roaming\Python\\ python27 \site-packages\flask_sqlalchemy\__init __。py,第856行,在create_all
self._execute_for_all_tables(app,bind,'create_all')
文件\AppData\Roaming \Python\Python27\site-packages\flask_sqlalchemy\__init __。py,第836行,在_execute_for_all_tables
app = self.get_app(app)
文件\AppData\Roaming \Python\Python27\site-packages\flask_sqlalchemy\__init __。py,第809行,在get_app
中引发RuntimeError('应用程序未在db
上注册'RuntimeError:应用程序未注册db
实例,没有绑定到当前上下文的应用程序

我做了一个关于这个话题的研究。这里建议重新考虑因素:



同样的问题在这里提出:

http://flask.pocoo.org/mailinglist/archive/2010/8/30/sqlalchemy-init-app-problem/#b1c3beb68573efef4d6e571ebc68fa0b



而上面的线程(2010)提出了这样的破解:

  app .register_blueprint(api)
db.app = app#< ------------<<
db.init_app(app)

有没有人知道如何做到这一点?你是怎么解决这个问题的?

谢谢

解决方案

请使用Flask的应用程序上下文。当使用 db.init_app(app)初始化时,Flask-SQLAlchemy不知道哪个应用程序是当前应用程序(请记住,Flask允许多个应用程序在同一个解释器中)。你可以有多个应用程序在同一个进程中使用相同的 SQLAlchemy 实例,而Flask-SQLAlchemy需要知道哪个是当前的(由于Flask的上下文本地一切的本质)。



如果你需要在运行时执行此操作,您必须明确地说出哪个应用程序是所有调用的当前应用程序。您可以通过更改您的代码来执行以下操作:

  def create_app():
app = flask。 Flask(app)
app.config ['SQLALCHEMY_DATABASE_URI'] ='sqlite://'
app.register_blueprint(api)
db.init_app(app)
with app.app_context():
像Flask-SQLAlchemy这样的扩展,现在知道当前的应用程序
#在这个块内。因此,您现在可以运行........
db.create_all()

返回应用程序






如果您正在编写需要应用程序上下文的独立脚本,则可以在开始时推送上下文,而不是将所有内容放在 with block。

  create_app()。app_context()。push )

如果您为Flask的 cli 该命令将自动访问上下文。


I am re-factoring my Flask application by scattering the models, blueprints but I am having a runtime error.

def create_app():
    app = flask.Flask("app")
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
    app.register_blueprint(api)
    db.init_app(app)
    db.create_all()
    return app

I have the following problem(the sample project are hosted here: https://github.com/chfw/sample):

Traceback (most recent call last):
  File "application.py", line 17, in <module>
    app = create_app()
  File "application.py", line 12, in create_app
    db.create_all()
  File "\AppData\Roaming\Python\Python27\site-packages\flask_sqlalchemy\__init__.py", line 856, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "\AppData\Roaming\Python\Python27\site-packages\flask_sqlalchemy\__init__.py", line 836, in _execute_for_all_tables
    app = self.get_app(app)
  File "\AppData\Roaming\Python\Python27\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

I did a research on this topic. The re-factoring is suggested here:

Flask-SQLAlchemy import/context issue

The same problem was raised here:

http://flask.pocoo.org/mailinglist/archive/2010/8/30/sqlalchemy-init-app-problem/#b1c3beb68573efef4d6e571ebc68fa0b

And the above thread(2010) suggested a hack like this:

    app.register_blueprint(api)
    db.app=app #<------------<<
    db.init_app(app)

Did anyone know how to do this properly? How did you solve it?

Thanks

解决方案

This has to do with Flask's application context. When initialized with db.init_app(app), Flask-SQLAlchemy doesn't know which app is the "current" app (remember, Flask allows for multiple apps in the same interpreter). You could have multiple apps using the same SQLAlchemy instance in the same process, and Flask-SQLAlchemy would need to know which is the "current" one (due to Flask's context local nature of everything).

If you need to do this during runtime, you must explicitly say which app is the "current" app for all calls. You can do this by changing your code to do the following:

def create_app():
    app = flask.Flask("app")
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
    app.register_blueprint(api)
    db.init_app(app)
    with app.app_context():
        # Extensions like Flask-SQLAlchemy now know what the "current" app
        # is while within this block. Therefore, you can now run........
        db.create_all()

    return app


If you are writing a standalone script that needs the app context, you can push the context at the beginning rather than putting everything in a with block.

create_app().app_context().push()

If you write a command for Flask's cli the command will automatically have access to the context.

这篇关于当分散烧瓶模型时,RuntimeError:'应用程序未注册到数据库'被引发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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