scan, list, describe, hover, er and nplusone, and the MCP tools list_apps,
list_models, describe_model, find_relations, er_diagram and nplusone_scan,
now read SQLAlchemy models as well as Django ones. Static: ast only, no
import, no database.
- sqlalchemy_parser: SQLAlchemy 2.0 DeclarativeBase / Mapped[], 1.x
declarative_base() / Column, Flask-SQLAlchemy db.Model, Flask-AppBuilder
Model and SQLModel table=True classes. Bases, abstract classes and mixins
are followed across files by import, including aliased imports from files
with no SQLAlchemy in them. Table names from __tablename__, __table__,
a declared_attr rule on the base, or the Flask / SQLModel defaults.
ForeignKey columns become relations to the class mapped to the target
table; relationship() attributes are kept apart with their lazy loader
and shape (one or many); a secondary= relationship is also a
ManyToManyField through the association table.
- ParsedModel gains orm, table_name and relationships (ParsedRelationship);
only SQLAlchemy models add them to their JSON.
- scan_workspace(include_sqlalchemy=False): off by default, so drift,
blast-radius, cascade, signals and suggest-indexes stay Django-only.
The SQLAlchemy walk runs only when there are no Django models or the
project declares SQLAlchemy in a dependency file (PostHog: 1.9s, not 9s).
- sqlalchemy_nplusone: loops over session.scalars(select(X)),
execute().scalars(), session.query(X), X.query, paginate(), rows unpacked
per entity, a single object's collection, nested loops and
comprehensions. Loader options (selectinload, joinedload, subqueryload,
immediateload, contains_eager, raiseload, noload, defaultload chains,
Load(), string paths, "*") count as loaded; non-lazy relationship
loaders are never reported. scan_all_nplusone runs each ORM's detector
on its own models and keeps Django advice off SQLAlchemy code.
- Fixed: a models.py that is SQLAlchemy code without a Django import was
read as Django models (65 phantom models in Superset, 138 in Polar, 69
in Dispatch).
- er_formats: SQLAlchemy tables show mixin columns and their real primary
key; DBML maps NO ACTION.
- workspace: alembic.ini or a sqlalchemy/sqlmodel dependency marks a
project root for the MCP server.
Census: Superset, Airflow, Prefect, Polar, Dispatch, Mealie, CTFd, Redash
and the FastAPI template give 517 models and 12 N+1 findings (7 high,
5 medium), all real on review. The six Django projects give no SQLAlchemy
models and unchanged Django models and N+1 findings.
What
scan,list,describe,hover,erandnplusone, and the MCP toolslist_apps,list_models,describe_model,find_relations,er_diagramandnplusone_scan, now read SQLAlchemy models alongside Django ones. Static, like the rest:astonly, nothing imported, no database.Docs:
docs/rules/sqlalchemy.mdRecognised
class Base(DeclarativeBase)declarative_base(),registry().generate_base(),@as_declarative(); or@registry.mappeddb.Model/ FABModel(auto snake_case table names)table=True(non-table parents lend fields)__abstract__classes and mixins are followed across files, including aliased imports from files with no SQLAlchemy in them (Superset'sfrom superset_core... import Dashboard as CoreDashboard).__tablename__,__table__,@declared_attrrules on the base (Prefect), Flask / SQLModel defaults, single-table inheritance.ForeignKeycolumns are ER edges to the class mapped to the target table;relationship()attributes are listed apart with theirlazyloader and shape;secondary=is a many-to-many through the association table, drawn once.N+1
Loops over
session.scalars(select(X)),execute(...).scalars(),session.query(X),X.query,paginate(), rows unpacked per entity, a single object's collection, nested loops and comprehensions. A relationship counts as loaded when the query's.options(...)names it (selectinload,joinedload, chains,defaultload,Load(), string paths,"*") or its loader is non-lazy. Collections arehigh, many-to-onemedium(the identity map may answer). Unreadable options, queries rebound in a loop, writes, method calls, tests and Alembic revisions stay quiet.Safety for Django users
scan_workspace(include_sqlalchemy=False)by default: drift, blast-radius, cascade, signals and suggest-indexes never see SQLAlchemy models.scan: 1.9 s, not 9 s.Fixed along the way: a
models.pythat is SQLAlchemy code without a Django import was read as Django models — 65 phantom models in Superset, 138 in Polar, 69 in Dispatch, 10 in Airflow, 8 in Redash.Census
All 12 findings were checked by hand: each is a real query per row. ER builds in all five formats on every project. The six Django projects (NetBox, PostHog, authentik, Plane, paperless-ngx, Healthchecks) get no SQLAlchemy models, and their Django models and N+1 findings are unchanged.
Tests
test_sqlalchemy_parser.py(17) andtest_sqlalchemy_nplusone.py(19); full suite 1091 passed, ruff and mypy clean.Summary by CodeRabbit