SQLAlchemy
Python SQL toolkit and object-relational mapper for database abstraction.
3. Used by agents' projects
Dependency in 181 public GitHub projects set up for AI coding agents that we scanned (2.9%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
From the package registry and GitHub, checked daily. Tap a verdict to see its source.How we checkedReport an error
What SQLAlchemy does
SQLAlchemy splits into a Core SQL toolkit that stands on its own and an ORM layered on top, and its maintainers insist the ORM must not hide the relational database, so every join, subquery and column stays the developer's decision.
SQLAlchemy popularity
Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated
How SQLAlchemy is used
- SQLAlchemy is a dependency in 181 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (11.2% of the 1,623 Python ones), #5 of 120 in databases, orms and clients.
- Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 1,623; those are left out of every share on this page.
- The most used alternative in our scan is DuckDB (26 projects).
- Projects that use it most often also use FastAPI (76.2%), Uvicorn (76.2%), Pydantic (71.3%).
- It is more common in projects set up for Roo Code (7.2%) than for OpenAI Codex (1.5%).
By coding agent
Often used with
SQLAlchemy in practice
Strengths
The Core is not just plumbing for the ORM. The README calls it a full database abstraction layer "in its own right". It has its own expression language, schema metadata, connection pooling and custom types.
The README says writing ORM queries "uses the same techniques of relational composition you use when writing SQL." The maintainers add that you keep full control over how joins are organized, how subqueries and correlation are used, and which columns are requested.
The maintainers' stated rule is to never render a literal value in a SQL statement. They say bound parameters let query optimizers cache query plans and make SQL injection "a non-issue".
One step reads an existing database schema into Python metadata objects. The same objects can then generate CREATE statements. The README says both happen in the Core, independent of the ORM.
Watch out for
By the maintainers' own description, the developer makes every decision about the design, structure and naming of both the object model and the schema. SQLAlchemy only provides the means to automate those decisions.
The README says that with the ORM, nothing goes to permanent storage until <code>commit()</code> is called. It presents this as part of making transactions the norm.
The 2.1.4 notes fix a regression where iterating a <code>Result</code> directly, or using methods such as <code>Result.scalars()</code>, created reference cycles. These kept the <code>Result</code> and its DBAPI cursor alive until cyclic GC ran. The notes say this came from the 2.1 refactoring of <code>Result</code> into Cython code.
More about SQLAlchemy
Funding
- GitHub Sponsors: https://github.com/sqlalchemy
- Patreon: https://patreon.com/zzzeek
- Tidelift: https://tidelift.com/funding/github/pypi/SQLAlchemy
- 23 GitHub sponsors
Agent Skills for it
- sqlalchemy-alembic-expert-best-practices-code-review: SQLAlchemy ORM and Alembic migration best practices for building safe, performant database schemas. This skill should be used when writing,
Packages around it
- python-dotenv: Loads .env config
- pydantic: Data validation
- fastapi: Web API framework
- uvicorn: ASGI server
- requests: HTTP client
- alembic: Schema migrations
More in Databases, ORMs and clients
Alternatives
SQLAlchemy This page
Embedded SQL OLAP database for analytical queries and data processing in Python.
Official Python driver for Neo4j graph database with Bolt protocol.
SQL ORM combining SQLAlchemy with Pydantic for Python databases.
Official MongoDB Python driver for querying and managing MongoDB databases.
Get started
Install it.
pip install sqlalchemyRules for your agent
## SQLAlchemy
- Check which line the project pins. The site lists both 2.1.4 and 2.0.54 under "Current Releases", each with its own migration notes and docs. The "Current Documentation" link is for 2.1.
- Never put literal values into SQL statements. Pass them as bound parameters, which the maintainers say lets query plans be cached and makes SQL injection "a non-issue".
- With the ORM, call `commit()` to persist changes. Nothing reaches permanent storage before that.
- If the task does not need an ORM, use the Core expression language instead. The README says: "Don't use an ORM if the problem doesn't need one."
- Express ORM queries with joins, subqueries and correlation in terms of the object model. Literal SQL is available, but the README says it is "virtually never needed".
- On the 2.1 line, use 2.1.4 or later. It fixes `Result` reference cycles introduced by the 2.1 Cython refactor and a connection leak with asyncio drivers.
- On a mapped Declarative class, assign only a `MetaData` object to the `metadata` attribute. Other values trigger a warning that the name should be left reserved.