# SQLTy vs SQLAlchemy

They solve different halves of one problem, and many projects use both. [SQLAlchemy](<https://www.sqlalchemy.org>) 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 `NUMERIC` to `Decimal`), 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, but `conn.execute(text(q))` still returns SQLAlchemy's untyped `Row`, 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](<https://sqlty.kuncar.dev/guide/drivers-and-orms/index.md>).

## 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](<https://sqlty.kuncar.dev/recipes/alembic/index.md>).

## Sources

- SQLAlchemy 2.1.9; `scripts/compat/reports/sqlalchemy.md` in the SQLTy repository: 133 match, 36 differ, 12 SQLTy gaps, 54 not comparable
- [Execute raw SQL: SQLAlchemy `text()`](<https://sqlty.kuncar.dev/guide/execute-raw-sql/index.md>), [Alembic recipe](<https://sqlty.kuncar.dev/recipes/alembic/index.md>)
