SQLTy vs sqlc¶
Both read your CREATE TABLE statements and give you typed queries without a database.
sqlc generates code: it reads annotated .sql files and writes the
functions and models that run them, in Go natively and in Python through a plugin
(sqlc-gen-python or
sqlc-gen-better-python). SQLTy
generates nothing. The query stays a string in your code (or a named query in the same .sql
files), and sqlty check compares it with the row type you wrote.
Quick comparison¶
| sqlc 1.31 with a Python plugin | SQLTy 0.10 | |
|---|---|---|
| Where queries live | Annotated .sql files (-- name: GetUser :one) |
String literals in Python, or the same annotated .sql files |
| Output | Generated Python modules, regenerated on every change | No generated code: SQL[T] annotations that sqlty rewrite writes into your source |
| Row type | A generated dataclass, or a Pydantic, attrs or msgspec model | Your own tuple, TypedDict, NamedTuple, dataclass or Pydantic model, or one rewrite writes with row-style |
| Driver | sqlc-gen-python: SQLAlchemy; sqlc-gen-better-python: asyncpg, psycopg, sqlite3, aiosqlite, asyncmy, PyMySQL | Any driver that takes a str: sqlite3, psycopg, rustmy, SQLAlchemy text(), Django; asyncpg through sqlty.drivers.asyncpg |
| Types come from | CREATE TABLE; optionally a PostgreSQL database for harder queries |
CREATE TABLE, migrations and casts; never a database |
| Engines typed | sqlc-gen-python types PostgreSQL only (other engines give Any) |
PostgreSQL, MySQL, SQLite; DuckDB best effort |
| Dynamic SQL | One query per annotation; sqlc.slice, sqlc.narg |
Strings built in if/else branches, one check per variant (preview) |
| Lint | sqlc vet: rules you write in CEL |
Over 70 built-in rules, many with fixes |
| Breaking changes | sqlc verify, against queries pushed to sqlc's hosted service |
sqlty breaking, against a git revision or a snapshot, locally |
| Other languages | Go natively; Kotlin, TypeScript and more through plugins | Python; Go in preview, as a go vet analyzer for pgx v5; TypeScript and JavaScript templates, as an ESLint plugin |
| Editor | Generated code is plain Python | Language server, VS Code extension, playground |
The same query in both¶
sqlc, with schema.sql, query.sql and a codegen entry for the Python plugin
in sqlc.yaml:
-- name: GetUser :one
SELECT id, name, email FROM users WHERE id = $1;
sqlc generate writes a models.py with a User dataclass, and code that runs
the query and builds a User from the row: a Querier class with a get_user
method (sqlc-gen-python) or a get_user function (sqlc-gen-better-python).
SQLTy, with schema = "schema.sql" under [tool.sqlty]:
schema.sql:
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT);
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")
Open in playgroundsqlty check . compares User with the columns the query returns. You run
GET_USER with your driver, and the psycopg helpers return
User rows typed end to end.
Where SQLTy is stronger¶
- No build step. Nothing to regenerate, review or keep in sync, and one calling convention
for all your queries: the query is a
strsubclass, so almost any driver takes it. - Your types, your drivers. Rows are whatever class you already use, and mypy, Pyright and ty see them through the psycopg helpers.
- Checks beyond types. Fan-out aggregates, joins on the wrong key, nullability through joins, parameter counts and types at each call, and migration safety.
- MySQL and SQLite rows are typed, where sqlc-gen-python falls back to
Any(source).
Where sqlc is stronger¶
- One query file, many languages. A Go service and a Python service can share one
query.sqland get native code in each. - Generated call signatures. The compiler checks arguments without a separate checker.
- Maturity. sqlc has been around since 2019, is widely adopted in Go, and features on the Thoughtworks Technology Radar.
- Batch and COPY helpers (
:batchexec,:copyfrom) in Go.
Which should you choose?¶
Choose sqlc if:
- your queries are fixed and callers only need a function to call
- you share one set of queries across Go, Kotlin or TypeScript services
- you prefer reviewing generated code to annotating inline strings
Choose SQLTy if:
- you want SQL inline, next to the code that uses it
- you already have row classes, or a driver sqlc's plugins don't support
- you want lint rules and breaking-change checks across a rolling deploy
Use both: SQLTy reads sqlc's query files (query-format = "sqlc"), so the Python side of a
polyglot repo can check the same .sql files sqlc generates Go from.
In Go, sqlc generates the functions you call, while SQLTy for Go (preview) checks
sqlty.SQL[R, P] constants and your own structs in go vet, with no generated code. It runs
them with pgx v5 through pgxq, and supports only PostgreSQL.
What SQLTy is missing¶
- Code generation: there is no function to call, so you pass the query to your driver.
- Named row classes for every query:
rewritewrites one only for a module-level query thatsqlty.drivershelpers alone run (row-style); for the others you write the class yourself (#1354). - Cardinality: sqlc's
:one/:manyis declared, while SQLTy doesn't yet reportfetchone()on a query that returns many rows.
Common questions¶
Can SQLTy check my existing sqlc queries?¶
Yes, with query-format = "sqlc":
[tool.sqlty]
schema = "schema.sql"
queries = ["query.sql"]
query-format = "sqlc"
schema.sql:
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL, email TEXT);
-- name: GetUser :one
-- error: [unknown-column] "column `emial` does not exist in `users`; did you mean `email`?"
SELECT id, name, emial FROM users WHERE id = $1;
@name, sqlc.arg(name) and sqlc.narg(name) are read as parameters, and
sqlc.embed(t) as the columns of t. See Queries in .sql files.
Does either need a database?¶
No. sqlc can use one for harder queries and for vet rules that prepare statements. SQLTy never
connects to one.
Sources¶
- sqlc docs: generate, language support,
vet,verify, macros (sqlc 1.31.1) - sqlc-gen-python, sqlc-gen-better-python
- sqlc on the Technology Radar