Flask-SQLAlchemy错误:'找不到应用程序。在视图函数内部工作或按下'

时间:2017-12-28 21:05:02

标签: python-3.x postgresql sqlalchemy flask-sqlalchemy greenlets

我正在尝试使用 flask-sqlalchemy ,我在如何获取Flask应用程序设置的文档中遵循了大量示例。但是,由于某种原因,我无法获取应用程序上下文。我目前正在使用app.app_context().push()方法。有没有其他方法让这个工作?我究竟做错了什么?任何帮助将不胜感激。

目前正在运行:

Python==3.6.3
Flask==0.12.2
Flask-Compress==1.4.0
Flask-SQLAlchemy==2.3.2

model.py

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import relationship


db = SQLAlchemy()


class Property(db.Model):
    __tablename__ = 'vt_property'

    id = db.Column(db.BigInteger, primary_key=True)
    name = db.Column(db.String(32))
    value = db.Column(db.LargeBinary)
    client_tag = db.Column(db.String(16))

index.py

from flask import Flask, jsonify, request
from flask_compress import Compress

from database.model import *
from manager.some_util import some_user, some_url, some_pass


# App config.
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
Compress(app)
app.secret_key = secret_key
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://connection_string'
db.init_app(app)
app.app_context().push()

some_util.py

from database.model import Property
from helpers.util import secret_key


def load_some_config():
    from secure.secure import read_encrypted_text
    some_configs = Property.query.filter(Property.name.contains('MANAGER')).all()
    some_url = ''
    some_user = ''
    some_pass = ''
    for c in some_configs:
        if 'URL' in c.name:
            some_url = c.value.decode('utf-8')
        if 'USER' in c.name:
            some_user = c.value.decode('utf-8')
        if 'PASS' in c.name:
            some_pass = read_encrypted_text(c.value).decode('utf-8')
    return some_url, some_user, some_pass

当我尝试运行API时,出现此错误:

*/venv/bin/python */src/api/index.py
Traceback (most recent call last):
  File "*/venv/lib/python3.6/site-packages/sqlalchemy/util/_collections.py", line 988, in __call__
    return self.registry[key]
KeyError: <greenlet.greenlet object at 0xXXXXXXXX>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "*/src/api/index.py", line 16, in <module>
    from manager.some_util import some_user, some_url, some_pass
  File "*/src/manager/some_util.py", line 23, in <module>
    some_url, some_user, some_pass = load_some_config()
  File "*/src/manager/some_util.py", line 9, in load_some_config
    some_configs = Property.query.filter(Property.name.contains(‘MANAGER’)).all()
  File "*/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 514, in __get__
    return type.query_class(mapper, session=self.sa.session())
  File "*/venv/lib/python3.6/site-packages/sqlalchemy/orm/scoping.py", line 78, in __call__
    return self.registry()
  File "*/venv/lib/python3.6/site-packages/sqlalchemy/util/_collections.py", line 990, in __call__
    return self.registry.setdefault(key, self.createfunc())
  File "*/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2882, in __call__
    return self.class_(**local_kw)
  File "*/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 141, in __init__
    self.app = app = db.get_app()
  File "*/venv/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 912, in get_app
    'No application found. Either work inside a view function or push'
RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.

1 个答案:

答案 0 :(得分:0)

要在Flask应用程序上下文中执行一些代码,最简单的方法是:

with self.app.app_context():

    # ... your code here ...

请注意,app是Flask应用程序对象。变量名称可能与您的情况不同。