SQLTy vs SQLAlchemy¶
They solve different halves of one problem, and many projects use both.
SQLAlchemy maps tables to Python classes and builds SQL from
Python expressions, with types from its 2.0 Mapped[...] annotations. SQLTy types the raw
SQL you write yourself, including the SQL you run through SQLAlchemy's text(). SQLTy's inferred types are compared with SQLAlchemy's for 235 columns and expressions on PostgreSQL, MySQL
and SQLite, and the report is regenerated weekly.
Quick comparison¶
| SQLAlchemy 2.1 | SQLTy 0.10 | |
|---|---|---|
| You write | Models and Python expressions, or text() |
SQL strings |
| Types come from | Mapped[...] annotations on your models |
CREATE TABLE files and casts |
Raw SQL through text() |
Untyped rows | Checked: unknown columns, row types |
| Dynamic queries | First class | if/else branches (preview) |
| Migrations | Alembic | Reads Alembic's SQL output as the schema, and lints it |
| Runtime | An ORM, connection pool and unit of work | Nothing: static analysis only |
Where SQLTy is stronger¶
- Raw SQL stays typed.
text()rows are untyped in SQLAlchemy, while SQLTy checks the query and its row type. - The schema is the source of truth, not a model that can drift from the migrations.
- SQL you can read in reviews, logs and
EXPLAIN. - No runtime layer on the request path.
Where SQLAlchemy is stronger¶
- Composition. Queries built from parts, relationships, eager loading.
- A unit of work for inserts and updates of object graphs.
- Portability across many databases from one query.
- Type conversions. SQLAlchemy converts some values (for example SQLite
NUMERICtoDecimal), and SQLTy types what the driver returns unless you annotate otherwise.
Which should you choose?¶
Choose SQLAlchemy's ORM for CRUD over object graphs and queries assembled at runtime.
Choose SQLTy for reporting queries, hot paths and anything that is clearer as SQL.
Use both: keep the ORM, and check the text() queries with SQLTy. Alembic migrations can feed
sqlty breaking through alembic upgrade --sql.
What SQLTy is missing¶
- Typed
text()results: SQLTy checks the query, butconn.execute(text(q))still returns SQLAlchemy's untypedRow, so you build your row type yourself (User(*row)). - Parameter checks on
conn.execute(text(...), {...})calls. - SQLAlchemy's value conversions: 36 of 235 rows differ, all documented in Drivers and ORMs.
Common questions¶
Does SQLTy work with Alembic?¶
Yes. alembic upgrade --sql writes the SQL of your migrations, which sqlty breaking reads
as the schema; see the Alembic recipe.
Sources¶
- SQLAlchemy 2.1.9;
scripts/compat/reports/sqlalchemy.mdin the SQLTy repository: 133 match, 36 differ, 12 SQLTy gaps, 54 not comparable - Execute raw SQL: SQLAlchemy
text(), Alembic recipe