Answer a question

I want to run a job with Flask-APScheduler that queries a Flask-SQLAlchemy model. When the job runs, I get RuntimeError: application not registered on db instance and no application bound to current context. How can I run a job that queries the database.

from flask_apscheduler import APScheduler

scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()
from models import User

def my_job():
    user = User.query.first()
    print(user)

The error occurs during the query, before it can be printed. The database is working in the rest of the application for other queries.

I tried to add with app.app_context(): while setting up the extension but that didn't work.

with app.app_context()
    scheduler = APScheduler()
    scheduler.init_app(app)
    scheduler.start()

The full traceback is:

ERROR:apscheduler.executors.default:Job "run_in (trigger: interval[0:00:10], next run at: 2016-10-18 23:00:53 CEST)" raised an exception
Traceback (most recent call last):
  File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/apscheduler/executors/base.py", line 125, in run_job
    retval = job.func(*job.args, **job.kwargs)
  File "/Users/user/Documents/myfolder/myfolder/myfile.py", line 19, in myjob
    user = User.query.all()
  File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 454, in __get__
    return type.query_class(mapper, session=self.sa.session())
  File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 71, in __call__
    return self.registry()
  File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/sqlalchemy/util/_collections.py", line 878, in __call__
    return self.registry.setdefault(key, self.createfunc())
  File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 704, in create_session
    return SignallingSession(self, **options)
  File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 149, in __init__
    self.app = db.get_app()
  File "/Users/user/.virtualenvs/myfolder/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 845, in get_app
    raise RuntimeError('application not registered on db '
RuntimeError: application not registered on db instance and no application bound to current context

Answers

I fixed this by forcing the Flask-SQLAlchemy extension to be in single-app mode from within the app factory. This technically isn't correct and could cause issues elsewhere, but it fixed my specific issue.

def create_app():
    new_app = Flask(__name__)
    db.init_app(new_app)
    db.app = new_app
    return new_app
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐