Skip to content

Registering sqlalchemy engine events #4785

Answered by davidism
TitaniumHocker asked this question in Q&A
Discussion options

You must be logged in to vote

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(...):
        ...

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@TitaniumHocker
Comment options

@davidism
Comment options

@TitaniumHocker
Comment options

@TitaniumHocker
Comment options

Answer selected by TitaniumHocker
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants