我有一个正在开发中的应用程序,但是当我尝试使用 Gunicorn 运行它时,它给出了“sqlalchemy 扩展未注册”的错误。从我读过的内容来看,我似乎需要调用 app.app_context()
某处,但我不确定在哪里。我该如何解决这个错误?
# run in development, works
python server.py
# try to run with gunicorn, fails
gunicorn --bind localhost:8000 server:app
AssertionError: The sqlalchemy extension was not registered to the current application. Please make sure to call init_app() first.
server.py
:from flask.ext.security import Security
from database import db
from application import app
from models import Studio, user_datastore
security = Security(app, user_datastore)
if __name__ == '__main__':
# with app.app_context(): ??
db.init_app(app)
app.run()
application.py
:from flask import Flask
app = Flask(__name__)
app.config.from_object('config.ProductionConfig')
database.py
:from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
最佳答案
仅当您使用 python sever.py
启动您的应用程序时是 if __name__ == '__main__':
块命中,您在其中使用您的应用程序注册您的数据库。
你需要移动那条线,db.init_app(app)
,在那个街区之外。
关于nginx - 使用 Gunicorn 运行应用程序时未注册 SQLAlchemy 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30764073/