I've been using SQLAlchemy only so I can't say with peewee. My take is SQLAlchemy has a bit of steep learning on the engine/connections part that's why i use flask-sqlalchemy. It takes time to get used to it but so far it's kinda worthwhile once the project goes a bit large. My personal rule is , use ORM for OLTP/ CRUD, but just use raw SQL for OLAP and doing analysis, i think it's much clearing going for raw SQL there. Also, it saves time to sync database migration with flask-migrate or alembic. Resource wise it is ok and there are plenty solutions out there. ALso, chatgpt3 seems to be helpful in fixing/crafting SQLAlchemy, not sure about peewee. What i don't like -- As application/models becomes larger and larger, i've noticed the startup time seems to be much slower, not sure how to fix this part since deploying w/ gunicorn restart is definiltely slower. I'm using a legacy 1.4, and sqlalchemy is 2.x , there's a huge migration leap for these version but i don't have the bandwidth to migrate such large app anymore. Answer from qatanah on reddit.com
๐ŸŒ
Flask-SQLAlchemy
flask-sqlalchemy.readthedocs.io โ€บ en โ€บ stable
Flask-SQLAlchemy โ€” Flask-SQLAlchemy Documentation (3.1.x)
Flask-SQLAlchemy is an extension for Flask that adds support for SQLAlchemy to your application.
๐ŸŒ
Python Beginners
python-adv-web-apps.readthedocs.io โ€บ en โ€บ latest โ€บ flask_db1.html
Flask and Databases โ€” Python Beginners documentation
An ORM converts data between incompatible systems (object structure in Python, table structure in SQL database). SQLAlchemy is basically a bridge between Python and a SQL database. Flask-SQLAlchemy is an extension for Flask that adds SQLAlchemy to your Flask app.
Discussions

Is SQLAlchemy really that good?
I've been using SQLAlchemy only so I can't say with peewee. My take is SQLAlchemy has a bit of steep learning on the engine/connections part that's why i use flask-sqlalchemy. It takes time to get used to it but so far it's kinda worthwhile once the project goes a bit large. My personal rule is , use ORM for OLTP/ CRUD, but just use raw SQL for OLAP and doing analysis, i think it's much clearing going for raw SQL there. Also, it saves time to sync database migration with flask-migrate or alembic. Resource wise it is ok and there are plenty solutions out there. ALso, chatgpt3 seems to be helpful in fixing/crafting SQLAlchemy, not sure about peewee. What i don't like -- As application/models becomes larger and larger, i've noticed the startup time seems to be much slower, not sure how to fix this part since deploying w/ gunicorn restart is definiltely slower. I'm using a legacy 1.4, and sqlalchemy is 2.x , there's a huge migration leap for these version but i don't have the bandwidth to migrate such large app anymore. More on reddit.com
๐ŸŒ r/flask
28
13
July 16, 2024
python - Proper sqlalchemy use in flask - Stack Overflow
I am making a basic API for funsies with flask and sqlalchemy. I read in the sqlalchemy documentation that you should only ever have one engine with the connection string. How is that supposed to be More on stackoverflow.com
๐ŸŒ stackoverflow.com
Assistance with converting SQL to flask_sqlalchemy
Although it is possible to execute an SQL statement using SQLAlchemy, the conventional method involves creating separate models for animals, farms, and persons, and defining their relations within the models. You interact with the models and instances, and the SQL operations necessary happen "underneath the hood". Then when you retrieve an animal with id 1, you can access its relations. animal = Animal.get(1) print(animal.farm.person.name) More on reddit.com
๐ŸŒ r/learnpython
4
1
March 30, 2023
Using flask_sqlalchemy and cannot figure out why db.session.bind is None
Seems this may be expected behavior: https://stackoverflow.com/a/38375161 How exactly are you trying to query? More on reddit.com
๐ŸŒ r/flask
5
1
April 7, 2023
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ Flask-SQLAlchemy
Flask-SQLAlchemy ยท PyPI
Flask-SQLAlchemy is an extension for Flask that adds support for SQLAlchemy to your application.
      ยป pip install Flask-SQLAlchemy
    
Published ย  Sep 11, 2023
Version ย  3.1.1
๐ŸŒ
Reddit
reddit.com โ€บ r/flask โ€บ is sqlalchemy really that good?
r/flask on Reddit: Is SQLAlchemy really that good?
July 16, 2024 -

I've read a lot about SQLAlchemy and it seems to get a lot of hate for bad documentation or poor performance. I heard Peewee is a good alternative that's much more optimized and easier to use. The question is though, why is SQLAlchemy still so popular and widely used in industry? I heard it's the go-to for startups who choose Flask for their projects.
Personally I work with raw SQL queries The only advantage of SQLAlchemy I can sense is when defining tables and data, it's much easier to make the change, while with raw queries, you must modify each query string statement individually. For projects that use many tables with tons of columns that might be exhausting. But if the performance concerns are true, how does SQLAlchemy get away with it?

๐ŸŒ
SourceForge
sourceforge.net โ€บ projects โ€บ flask-sqlalchemy.mirror
Flask-SQLAlchemy download | SourceForge.net
September 11, 2023 - Download Flask-SQLAlchemy for free. Adds SQLAlchemy support to Flask. Flask-SQLAlchemy is an extension for Flask that adds support for SQLAlchemy to your application. It simplifies using SQLAlchemy with Flask by setting up common objects and ...
๐ŸŒ
FastAPI
fastapi.tiangolo.com โ€บ tutorial โ€บ sql-databases
SQL (Relational) Databases - FastAPI
As SQLModel is based on SQLAlchemy, you can easily use any database supported by SQLAlchemy (which makes them also supported by SQLModel), like:
Find elsewhere
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ SQLAlchemy
SQLAlchemy ยท PyPI
A Core SQL construction system and DBAPI interaction layer. The SQLAlchemy Core is separate from the ORM and is a full database abstraction layer in its own right, and includes an extensible Python-based SQL expression language, schema metadata, ...
      ยป pip install SQLAlchemy
    
Published ย  Apr 03, 2026
Version ย  2.0.49
๐ŸŒ
Stackademic
blog.stackademic.com โ€บ using-flask-with-sqlalchemy-for-database-integration-f2d7182ef0f1
Using Flask with SQLAlchemy for Database Integration | by Oludakevin | Stackademic
September 30, 2024 - In this article, we covered how to integrate SQLAlchemy with Flask to manage database operations. We created a simple User model and implemented CRUD operations to interact with the database.
๐ŸŒ
DEV Community
dev.to โ€บ skyaigrey โ€บ how-to-install-and-use-sqlalchemy-in-flask-4hl9
How to Install and Use SQLAlchemy in Flask - DEV Community
January 25, 2024 - In this blog post, you will learn how to install and use SQLAlchemy in Flask, and how to perform some common database operations, such as creating tables, inserting data, querying data, updating data, and deleting data.
๐ŸŒ
Python Geeks
pythongeeks.org โ€บ python geeks โ€บ learn flask โ€บ flask sqlalchemy
Flask SQLAlchemy - Python Geeks
May 11, 2023 - Flask SQLAlchemy is a popular library for performing database operations in Flask, a lightweight and flexible web framework for Python. SQLAlchemy, a comprehensive Object Relational Mapper (ORM), provides a powerful and efficient way to interact ...
๐ŸŒ
Real Python
realpython.com โ€บ flask-database
Enhance Your Flask Web Project With a Database โ€“ Real Python
January 4, 2024 - Adding a database to your Flask project elevates your web app to the next level. In this tutorial, you'll learn how to connect your Flask app to a database and how to receive and store posts from users.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ connect-flask-to-a-database-with-flask-sqlalchemy
Flask SQLAlchemy Tutorial for Database - GeeksforGeeks
October 24, 2025 - Flask doesnโ€™t have a built-in way to handle databases, so it relies on SQLAlchemy, a powerful library that makes working with databases easier.
๐ŸŒ
GitHub
github.com โ€บ pallets-eco โ€บ flask-sqlalchemy
GitHub - pallets-eco/flask-sqlalchemy: Adds SQLAlchemy support to Flask ยท GitHub
Flask-SQLAlchemy is an extension for Flask that adds support for SQLAlchemy to your application.
Starred by 4.3K users
Forked by 900 users
Languages ย  Python
๐ŸŒ
GoLinuxCloud
golinuxcloud.com โ€บ home โ€บ programming โ€บ flask sqlalchemy [in-depth tutorial]
Flask SQLAlchemy [In-Depth Tutorial] | GoLinuxCloud
January 9, 2024 - Flask SQLAlchemy is an extension for flask that adds support for SQLAlchemy to flask by adding useful defaults and helpers that simplify basic tasks
Top answer
1 of 2
18

Of course there is already a Flask extension Flask-SQLAlchemy - also mentioned in the Flask docs SQLAlchemy in Flask. But like most Flask extensions, all it does is "just" some "plumbing" Flask and SQLAlchemy (or any other library) together. The best documentation is often times the source code :)

github.com/mitsuhiko/flask-sqlalchemy/blob/master/flask_sqlalchemy/__init__.py

However, in case of Flask-SQLAlchemy it is a lot of code and also some black magic about scoped sessions intertwined with Flask contexts and modification tracking and duration debugging and some things useful in web apps and all possible corner cases and some other things. I'd say it's a bit overengineered. I'm not saying that you shouldn't use it - just that the connection between SQLAlchemy and Flask is not visible at first look from the extension code so it may take more reading time.

But if you wish to do it yourself it is very easy (well, as easy as SQLAlchemy can get) - just initialize SQLAlchemy so you get a sessionmaker, and then create a session before each request, then don't forget to close it after the request :) and just use this session in your Flask handlers (I mean the @app.route functions).

import flask
import sqlalchemy
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()

class Item (Base):

    __tablename__ = 'items'

    id = Column(Integer, primary_key=True)
    title = Column(String)    

engine = sqlalchemy.create_engine('sqlite:///bestappever.db', echo=True)
Session = sessionmaker(bind=engine)

# create all tables if you wish...
Base.metadata.create_all(engine)

app = flask.Flask(__name__)

@app.before_request
def create_session():
    flask.g.session = Session()

@app.teardown_appcontext
def shutdown_session(response_or_exc):
    flask.g.session.commit()
    flask.g.session.remove()

@app.route('/')
    # just use the SQLAlchemy session :)
    items = flask.g.session.query(Item).all()
    return flask.render_template('index.html', items=items)

See my example Flask + SQLAlchemy app: https://github.com/messa/db-workshop-web-app/blob/sqlalchemy/anketa.py

As you see you can even have everything on one big file but splitting it into multiple files (one of which is usually named like model.py) is the best practice.

The most important thing is to have isolated sessions between individual requests - in my solution the session is created for each request in the before_request hook. The solution from Flask docs "SQLAlchemy in Flask" uses scoped_session which has basically the same effect (achieved using thread-local variable with one separate session per each thread).

Ad application architecture: for bigger applications it is good to use Larger Applications patterns and blueprints. So all my flask route handlers will be in blueprints, and then I would have one "main" function get_app() that 1) creates Flask object 2) registers the blueprints with it 3) creates SQLAlchemy engine and hooks Session() with Flask app.before_request. Roughly similar to this or this.


you should only ever have one engine

Why? Technically, engine is just a connection (pool) to the database. What if your app uses three separate databases? Then you need three engines, of course.

But of course a session is connected to exactly one engine. So you would need multiple sessions. And multiple declarative bases for your model classes, and - most importantly - not mix it up accidentally. That's why it's recommended to have just one engine - if possible.

You can create engine per request - it is technically correct, but it is inefficient. Keep one engine for whole application and just create session per request.

2 of 2
0

Once you have SQLAlchemy installed on your Flask app you will need to create a separate *.py file to store the database configuration and use import statements to incorporate it into your other .py files to use.

If you want to see/create tables for your API data within the app itself, they would reside in something like a 'models.py' file which also imports the db connection string.

Here is a pretty good Flask + SQLAlchemy todo app tutorial, link is to the section where she describes the file structure and db implementation in detail.

http://www.vertabelo.com/blog/technical-articles/web-app-development-with-flask-sqlalchemy-bootstrap-part-2

Below is a link with a bit more generic however also detailed walk-thru regarding setting up a SQLAlchemy db with Flask, and also goes over a few examples of using ORM queries rather than raw SQL queries (although I generally use raw SQL queries - given that I usually also install MySQL & create tables in MySQL workbench, however this may be overkill for an app just using API data):

http://flask.pocoo.org/docs/0.12/patterns/sqlalchemy/

๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-use-flask-sqlalchemy-to-interact-with-databases-in-a-flask-application
Interact with Databases in Flask Using Flask-SQLAlchemy | DigitalOcean
October 21, 2025 - It also gives you an Object Relational Mapper (ORM), which allows you to make queries and handle data using simple Python objects and methods. Flask-SQLAlchemy is a Flask extension that makes using SQLAlchemy with Flask easier, providing you tools and methods to interact with your database ...
๐ŸŒ
Flask
flask.palletsprojects.com โ€บ en โ€บ stable โ€บ quickstart
Quickstart โ€” Flask Documentation (3.1.x)
Extensions are packages that help you accomplish common tasks. For example, Flask-SQLAlchemy provides SQLAlchemy support that makes it simple and easy to use with Flask.
๐ŸŒ
Rithm School
rithmschool.com โ€บ courses โ€บ flask-fundamentals โ€บ lessons โ€บ sql-alchemy-with-flask
SQL Alchemy with Flask
March 29, 2023 - Log in with your username and password or sign up for a free account to continue learning!