Open-source project
pallets-eco/flask-sqlalchemy avatar
pallets-eco/flask-sqlalchemy

Flask-SQLAlchemy: the defaults and helpers wrapped around SQLAlchemy

Adds SQLAlchemy support to Flask

4,301 stars905 forksPythonBSD-3-Clause

At a glance

What is it?
Flask-SQLAlchemy is a Pallets-Eco extension that bolts SQLAlchemy onto a Flask app, supplies useful defaults and a few helpers, and leaves the ORM itself intact. What the package metadata and the README example do not tell you is how much of the schema, session and database setup still lands on you.
Who is it for?
Take Flask-SQLAlchemy when you want a Flask app wired to SQLAlchemy 2 with a session and model base already configured, and use Alembic with Flask-Migrate or Flask-Alembic for schema migrations, and configure a supported database URI and driver.
Can I use it commercially?
Yes. BSD-3-Clause is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 179 days ago.
What is it written in?
Mainly Python, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 13, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • pallets-eco/flask-sqlalchemy4,301 ★ · -6
Star history
Repository2026-09-232026-11-13
pallets-eco/flask-sqlalchemy4.3k4.3k

-6 Stars in the last 51 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

The extension supplies defaults and helpers, not a different ORM

Flask-SQLAlchemy adds SQLAlchemy support to a Flask application. What it claims to change is ergonomics, not the ORM: it aims to simplify using SQLAlchemy with Flask by providing useful defaults and extra helpers that make common tasks easier. Everything you know about SQLAlchemy 2 still applies, because the package declares sqlalchemy>=2.0.16 as a runtime dependency and the shipped example imports DeclarativeBase, Mapped and mapped_column straight from sqlalchemy.orm. Two runtime dependencies in total, the other being flask>=2.2.5. The metadata is candid about the audience: classifiers for a production stable status, the web environment, OS independent operation and typed code. This is a bridge library. If your objection to SQLAlchemy is the API, this package does not answer it, and if your objection is the boilerplate around sessions and model bases, it does.

`db.Model` inherits the declarative base you hand the constructor

The example makes the base class explicit and passes it in. You declare Base(DeclarativeBase) yourself, construct the extension as SQLAlchemy(app, model_class=Base), and then define models on top of db.Model with SQLAlchemy 2 typed columns. The two columns in the example are annotated rather than cast: id: Mapped[int] = mapped_column(primary_key=True) and username: Mapped[str] = mapped_column(unique=True). Two consequences follow from the constructor signature. The extension does not impose its own declarative base, so if your project already has one, you keep it. And the typed column style in the example is SQLAlchemy 2 syntax, which lines up with the sqlalchemy>=2.0.16 floor in the project metadata and with requires-python = ">=3.8". Attribute typing works only for columns declared this way; a bare db.Column has no annotation to read.

`db.session` requires an active application context

Table creation and the first write both happen inside a context manager in the published example:

python
with app.app_context():
    db.create_all()

    db.session.add(User(username="example"))
    db.session.commit()

The session requires an active Flask application context. Requests and Flask CLI commands provide one automatically; standalone code can enter app.app_context() explicitly. create_all() is also the entire schema story in the example: it creates tables for the models that have been imported at that point, and the README does not document a migration path or a downgrade for this extension. create_all() does not update existing tables; the official guide recommends a migration library such as Alembic with Flask-Migrate or Flask-Alembic. The README then reads rows using `db.session.scalars(db.select(User))`.

`SQLALCHEMY_DATABASE_URI` is the one setting the example touches

The database is chosen in the Flask config, before the extension is constructed:

python
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///example.sqlite"

That single key is the whole connection story in the README. The example stores its data in a SQLite file called example.sqlite, which makes it run with no server and no driver beyond the standard library. A server database instead of a file means supplying a different URI, and the published example gives none for it: no connection arguments, no pool settings, no mention of environment variables.

flit builds one module called `flask_sqlalchemy` under BSD-3-Clause

The distribution is built by flit, and the mapping from package name to import name is written out in the project file:

toml
[build-system]
requires = ["flit-core<4"]
build-backend = "flit_core.buildapi"

[tool.flit.module]
name = "flask_sqlalchemy"

So the distribution is Flask-SQLAlchemy and the import is flask_sqlalchemy. The licence file is LICENSE.txt and the licence itself is BSD-3-Clause. Maintainers are listed as Pallets with [email protected]. The sdist configuration includes docs/, requirements/, tests/, CHANGES.rst and tox.ini, which tells you what travels with a source release: the documentation tree, the test suite and the change log, not just the module.

Dev, typing, docs and test work sit in dependency groups

Only two packages are needed to run the extension. Everything else is grouped:

toml
requires-python = ">=3.8"
dependencies = [
    "flask>=2.2.5",
    "sqlalchemy>=2.0.16",
]

The dev group pulls in ruff, tox and tox-uv, then includes the pre-commit, docs, tests and typing groups rather than repeating their contents. tests holds pytest. typing holds mypy, pyright and pytest, so a contributor is expected to satisfy two type checkers, not one. docs is pallets-sphinx-themes, sphinx and sphinxcontrib-log-cabinet. Two groups are conditional on the interpreter: gha-update only installs on python_full_version >= '3.12', and the pre-commit group only on python_full_version >= '3.9'. A lock file for the development environment is committed as uv.lock.

Pallets-Eco maintains it, with two example applications in the tree

The project states that it belongs to the Pallets Community Ecosystem, that Pallets maintains Flask, and that Pallets-Eco exists so the extensions can be maintained by the community. Anyone interested in helping maintain it is pointed at the Pallets Discord server, and the repository carries CONTRIBUTING.rst and CHANGES.rst. The top level of the repository is a documentation project as much as a library: docs/ for the manual published at flask-sqlalchemy.readthedocs.io, artwork/ for images, a .readthedocs.yaml for the hosting side and examples/ holding two working applications, flaskr/ and todo/. Those two directories are where the extension is used at length, and they are the first place to look if the README example leaves a question about query patterns or relationships unanswered.

The newest tag is 3.1.1 from September 2023

The release history is short and old: 3.1.1 and 3.1.0 were both published on 2023-09-11, and 3.0.5 on 2023-06-21 before that. The project file still says version = "3.1.1", so nothing has been cut since, even though the branch itself is not dormant: the last push to the repository was on 2026-05-18. That gap matters for anyone writing a dependency constraint, because the floors the extension asks for are flask>=2.2.5 and sqlalchemy>=2.0.16 and the Python floor is 3.8, so a resolver on a current Flask will install this alongside a SQLAlchemy 2 line that has moved on since the tag. Documentation is hosted separately at flask-sqlalchemy.readthedocs.io with its own changes page, and the source of truth for behaviour on a newer branch is that documentation rather than the last tag.

Editorial conclusion

Take Flask-SQLAlchemy when you want a Flask app wired to SQLAlchemy 2 with a session and model base already configured, and use Alembic with Flask-Migrate or Flask-Alembic for schema migrations, and configure a supported database URI and driver. Before pinning a version, check which release your resolver picks: the newest tag is 3.1.1 from 2023-09-11, the runtime floors are flask>=2.2.5 and sqlalchemy>=2.0.16, and pyproject.toml still declares version = "3.1.1" while the last push to the branch was on 2026-05-18.

Frequently asked questions

What are the differences between SQLAlchemy and Flask-SQLAlchemy?

SQLAlchemy is the ORM library; Flask-SQLAlchemy is the Flask extension that adds support for it. The extension aims to simplify SQLAlchemy in Flask by providing useful defaults and extra helpers, and it depends on sqlalchemy>=2.0.16 and flask>=2.2.5 without replacing any part of the ORM.

What is Flask-SQLAlchemy used for?

It is used to put SQLAlchemy into a Flask application: the app object and a model base are passed to SQLAlchemy(app, model_class=Base), models are declared on db.Model, and the session is used inside an app context to create tables and commit rows.

What is Flask-SQLAlchemy in Python?

It is a typed Python package distributed as Flask-SQLAlchemy and imported as flask_sqlalchemy, licensed BSD-3-Clause and built with flit-core. The project file marks it as production stable, OS independent and requires-python >= 3.8.

How do I install Flask-SQLAlchemy?

Install the package with python -m pip install Flask-SQLAlchemy. Check the selected release’s Python, Flask and SQLAlchemy requirements before deploying.

How do I use Flask-SQLAlchemy?

Set app.config["SQLALCHEMY_DATABASE_URI"], construct SQLAlchemy(app, model_class=Base), declare models on db.Model with Mapped and mapped_column columns, then call db.create_all() and use db.session inside with app.app_context(). The README example stores its data in a SQLite file at sqlite:///example.sqlite.

Official sources

  1. License: BSD-3-Clause
  2. pallets-eco/flask-sqlalchemy on GitHub
  3. Project website
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/pallets-eco-flask-sqlalchemy.svg)](https://hysenlabs.com/projects/pallets-eco-flask-sqlalchemy)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/pallets-eco-flask-sqlalchemy.svg)](https://hysenlabs.com/projects/pallets-eco-flask-sqlalchemy)

Was this useful?