RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.

137 views
Skip to first unread message

Roger Creus

unread,
Aug 10, 2021, 12:58:04 PM8/10/21
to APScheduler
Hello everybody,

I am using apscheduler in a flask application and I have deployed it to Heroku successfully. I have a "hello world" task to run every minute and it is triggered correctly. However, when I want this task to insert data to the flask SQLAlchemy database I get this error in the Heroku logs:

RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.

A hello world example like mine would be to define a model in models.py:


class HelloWorld(db.Model):
__tablename__ = 'helloWorld' id = db.Column(db.Integer, primary_key=True) name =         db.Column(db.String(10))

and then in clock.py:

from apscheduler.schedulers.blocking import BlockingScheduler

 sched = BlockingScheduler() @sched.scheduled_job('interval', minutes=1) 

def insert_new_name_to_helloWorld(): 
new_name = HelloWorld(name = "Hello") db.session.add(new_name) db.commit()


sched.start()

What Am i doing wrong? Thank you!

Alex Grönholm

unread,
Aug 10, 2021, 1:06:47 PM8/10/21
to apsch...@googlegroups.com

The most likely cause for this is that you tried to use flask-sqlalchemy without an application context (e.g. in a scheduled function). I suggest you use regular SQLAlchemy in scheduled tasks.

--
You received this message because you are subscribed to the Google Groups "APScheduler" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apscheduler...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/apscheduler/4b9ce7d0-08bc-4e1c-95d3-378d7862fbc7n%40googlegroups.com.

Roger Creus

unread,
Aug 19, 2021, 12:02:21 PM8/19/21
to APScheduler
I  am using flask-sqlalchemy right now . What is really the difference between regular SQLAlchemy and flask-sqlalchemy? Do I just have to change syntax?

El dia dimarts, 10 d’agost de 2021 a les 19:06:47 UTC+2, Alex Grönholm va escriure:

Alex Grönholm

unread,
Aug 19, 2021, 3:29:11 PM8/19/21
to apsch...@googlegroups.com

The crucial difference is that it requires an application context, which won't exist in a function scheduled with APScheduler. That is the problem you are facing now.

Reply all
Reply to author
Forward
0 new messages