Registering sqlalchemy engine events #4785
-
I need to register some event listeners on Is there any way to run callback just after application context is created? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
SQLAlchemy events are entirely part of SQLAlchemy. A Flask app context shouldn't be required to manage them. You should attach the events when creating the engine, not on every app context push. Push an app context to get access to Flask-SQLAlchemy's engine. The same engine (pool) is reused across contexts, even though it requires an active context to actually get it. with app.app_context():
engine = db.engine
@sa.events.listen_for(engine, "event_name")
def my_listener(...):
... |
Beta Was this translation helpful? Give feedback.
SQLAlchemy events are entirely part of SQLAlchemy. A Flask app context shouldn't be required to manage them. You should attach the events when creating the engine, not on every app context push.
Push an app context to get access to Flask-SQLAlchemy's engine. The same engine (pool) is reused across contexts, even though it requires an active context to actually get it.