Registry / web-framework / fastapi-sqlalchemy

fastapi-sqlalchemy

JSON →
library0.2.1pypypi✓ verified 91d ago

Provides a simple middleware and dependency to integrate SQLAlchemy sessions into FastAPI applications. Current version 0.2.1, last updated in 2021. Low release cadence; considered stable but minimal maintenance.

pip install fastapi-sqlalchemy
INSTALL
IMPORT
SIG · FASTAPI-SQLALCHEMY
F
fastapi-sqlalchemy
web-frameworkpythonv0.2.1
Install
3.5s avg
Import
938ms
Disk
44MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.2.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.978s · 45.4MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 3.5s · import 0.898s · 44MB
44MB installed
● package 44MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DBSessionMiddleware
✓ from fastapi_sqlalchemy import DBSessionMiddleware
✗ from fastapi_sqlalchemy.middleware import DBSessionMiddleware
DBSessionMiddleware is exposed at package level
db
✓ from fastapi_sqlalchemy import db
✗ from fastapi_sqlalchemy.db import db
The db object is a session proxy, import directly from package

Initialize middleware, then use `db.session` for queries.

import os from fastapi import FastAPI from fastapi_sqlalchemy import DBSessionMiddleware, db from sqlalchemy import create_engine app = FastAPI() DATABASE_URL = os.environ.get('DATABASE_URL', 'sqlite:///test.db') app.add_middleware(DBSessionMiddleware, db_url=DATABASE_URL) @app.get('/') def root(): # db.session is the current SQLAlchemy session result = db.session.execute('SELECT 1').scalar() return {'result': result}
Debug
Known issues
gotchaThe `db` object is a thread-local proxy and must be accessed within the request lifecycle. Using it outside of a request context (e.g., at module level) will raise an error.
fix
Always access `db` inside a FastAPI route or dependency.
affects: all
deprecatedThe library is not actively maintained (last release 2021) and does not support newer SQLAlchemy 2.0 style. It uses the deprecated `Session` pattern.
fix
Consider switching to FastAPI's built-in SQLAlchemy support with `Session` dependency injection or use sqlalchemy.ext.asyncio for async.
affects: >=0.2.0
gotchaThe middleware does not handle database disconnections or connection pooling gracefully by default. Long-running applications may experience stale connections.
fix
Manually configure engine with `pool_pre_ping=True` using `engine_args` parameter in middleware.
affects: all
Errors
Common errors & fixes
RuntimeError: No database session found. Have you added the DBSessionMiddleware?
Accessing `db.session` before the middleware has been added or outside a request context.
fix
Ensure `app.add_middleware(DBSessionMiddleware, db_url=...)` is called before any route that uses `db`.
AttributeError: module 'fastapi_sqlalchemy' has no attribute 'DBSessionMiddleware'
Importing from the wrong module path or outdated pip package.
fix
Use `from fastapi_sqlalchemy import DBSessionMiddleware` (no submodule).
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: ...
Tables not created because the middleware does not auto-create tables.
fix
Use `Base.metadata.create_all(bind=engine)` before starting the app, using the same engine URL.
Upgrade
Version history
0.2.1latest on PyPI · released Sep 25, 2020
Audit
Dependencies
fastapirequiredRequired to run FastAPI app
sqlalchemyrequiredCore ORM library
pydanticrequiredShared dependency with FastAPI
Agent activity
7 hits · last 30 days
node
6
Resources
fastapi-sqlalchemy — pip install fastapi-sqlalchemy · libregistry