# SQLTy vs SQLx

Both check raw SQL before it runs, and both keep the query as you wrote it. [SQLx](<https://github.com/transact-rs/sqlx>) does it for Rust: its `query!` macros send each query to a database at compile time and turn the answer into types. SQLTy does it for Python, reading the types from your schema files instead of asking a database. They aren't alternatives within one language. This page is for teams who know SQLx and want the same guarantee in Python, or who run both languages.

## Quick comparison

|  | SQLx 0.9 | SQLTy 0.10 |
| --- | --- | --- |
| Language | Rust | Python (the native `sqlty` executable checks any Python 3 project; the annotations need Python 3.14) |
| When it checks | `cargo build`, in the macro expansion | `sqlty check`, the language server, pre-commit or CI |
| Types come from | A live database at `DATABASE_URL`, or the `.sqlx` cache that `cargo sqlx prepare` writes from one | `CREATE TABLE` statements in a schema dump or migrations, and casts |
| Row type | `query!`: an anonymous record; `query_as!`: your struct | `SQL[T]`: a tuple, `TypedDict`, `NamedTuple`, dataclass or Pydantic model |
| Nullability | From `NOT NULL`; PostgreSQL expressions assumed nullable; forced with `"id!"` / `"id?"` | From `NOT NULL`, keys, joins, `WHERE ... IS NOT NULL` and expressions |
| Type overrides | `sqlx.toml` `[macros.type-overrides]` | `[tool.sqlty.types]`, by SQL type or `table.column` |
| Databases | PostgreSQL, MySQL, SQLite | PostgreSQL, MySQL, SQLite; DuckDB best effort |
| Migrations | `sqlx migrate` runs them | Reads them as the schema; `sqlty breaking` checks both directions of a deploy |
| Runtime | SQLx is also the driver and pool | Not a driver: use sqlite3, psycopg, asyncpg, rustmy |

## The same query in both

SQLx, with `DATABASE_URL` set at build time:

```rust
struct User {
    id: i32,
    name: String,
    email: Option<String>,
}

let user = sqlx::query_as!(User, "SELECT id, name, email FROM users WHERE id = $1", id)
    .fetch_one(&pool)
    .await?;
```

SQLTy, with `schema = "schema.sql"` under `[tool.sqlty]`:

`schema.sql`:

```sql
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT);
```

```python
from dataclasses import dataclass

from sqlty import SQL


@dataclass
class User:
    id: int
    name: str
    email: str | None


GET_USER = SQL[User]("--sql\nSELECT id, name, email FROM users WHERE id = %s")
```

`sqlty check .` compares `User` with the columns the query returns, as `query_as!` compares the fields of your struct. `sqlty rewrite .` writes `T` for a query that has none yet, as `query!` builds an anonymous record.

## Where SQLTy is stronger

- **No database at check time.** CI, editors and the playground check queries with nothing running. SQLx needs a server with the current schema whenever a query changes, or a committed `.sqlx` cache that `cargo sqlx prepare --check` keeps fresh.
- **Nullability through expressions and joins**, where SQLx makes PostgreSQL expressions nullable and asks for `!` overrides.
- **Lint rules** (fan-out aggregates, wrong join keys, `SELECT *`) and **`sqlty breaking`** for old code against the new schema and new code against the old one.
- **`sqlty format`** formats the SQL inside strings.

## Where SQLx is stronger

- **The database is the oracle.** Types are what the server says, including functions, extensions and views SQLTy has never heard of.
- **Driver included.** One crate checks, runs, pools and migrates.
- **The compiler enforces it.** A wrong query doesn't build; SQLTy relies on running a check step in CI.

## Which should you choose?

Choose SQLx if you write Rust. SQLTy doesn't check Rust code yet ([\#855](<https://github.com/jirikuncar/sqlty/issues/855>)).

Choose SQLTy if you write Python and want SQLx's guarantee without a database in the loop.

Use both: a Rust service on SQLx and a Python service on SQLTy can share the migrations directory. SQLx runs it, and SQLTy reads it as the schema.

## What SQLTy is missing

- Rust support ([\#855](<https://github.com/jirikuncar/sqlty/issues/855>)).
- A check against a live database: a schema file that drifts from production goes unnoticed.

## Common questions

### Is SQLTy SQLx for Python?

For the checking half, yes: raw SQL in string literals, checked before it runs, with row types for your type checker. SQLTy isn't a driver, though.

### Can SQLTy use a database like SQLx does?

No, by design: `sqlty check` never connects, so it runs the same everywhere.

### Where does the configuration go?

SQLx 0.9 reads crate-wide settings from one file:

`sqlx.toml`:

```toml
[common]
database-url-var = "APP_DATABASE_URL"

[macros.type-overrides]
'UUID' = "crate::types::MyUuid"
```

SQLTy reads them from `[tool.sqlty]`:

```toml
[tool.sqlty]
dialect = "postgres"
schema = ["schema.sql", "migrations/"]

[tool.sqlty.types]
"users.id" = "app.types.UserId"
```

See [Configuration](<https://sqlty.kuncar.dev/reference/configuration/index.md>) for every key.

## Sources

- SQLx 0.9.1 (crates.io), [0.9.0 release notes](<https://github.com/transact-rs/sqlx/discussions/4271>)
- [`query!`](<https://docs.rs/sqlx/latest/sqlx/macro.query.html>), [`query_as!`](<https://docs.rs/sqlx/latest/sqlx/macro.query_as.html>), [`sqlx.toml` reference](<https://docs.rs/sqlx/latest/sqlx/_config/_reference/index.html>)
