flask-sqlalchemy or sqlalchemy

The main feature of the Flask-SQLAlchemy is proper integration with Flask application – it creates and configures engine, connection and session and configures it to work with the Flask app.

This setup is quite complex as we need to create the scoped session and properly handle it according to the Flask application request/response life-cycle.

In the ideal world that would be the only feature of Flask-SQLAlchemy, but actually, it adds few more things. Check out the docs for more info. Or see this blog post with the overview of them: Demystifying Flask-SQLAlchemy (update: the original article is not available at the moment, there is a snapshot on webarchive).

When I first worked with Flask and SQLAlchemy, I didn’t like this overhead . I went over and extracted the session management code from the extension. This approach works, although I discovered that it is quite difficult to do this integration properly.

So the easier approach (which is used in another project I am working on) is to just drop the Flask-SQLAlchemy in and don’t use any of additional features it provides. You will have the db.session and you can use it as if it was pure SQLAlchemy setup.

Leave a Comment