sqlc
Tool that generates type-safe Go code from SQL queries and schema files.
Official Documentation ↗sqlc v1.26+ / Go 1.20+. Check the official docs for the latest API surface.Installation
go get github.com/sqlc-dev/sqlcMethods
No methods match this filter.
Overview
sqlc compiles SQL into type-safe Go. You define schema and queries, annotate result shapes, and sqlc generates structs and methods that match the database types.
Core Methods
Provide sqlc.yaml, schema and query files, run generate, and use the generated Queries type. WithTx binds queries to a transaction, and generated methods accept the configured context.
Usage Notes
Keep the schema in sync with migrations so generated types stay correct, and commit generated code so builds are reproducible. Review generated diffs when types change.
Common Pitfalls
A stale schema produces wrong types that silently miscast values. Unsupported SQL constructs require rewrites or config tweaks. Generated code should not be edited by hand, since regeneration overwrites it.
API reference evidence
Fetched from official sources on 2026-11-08T09:53:13.312Z via WebFetch https://docs.sqlc.dev/en/latest/howto/generate.html.
q.WithTx→ signature from https://docs.sqlc.dev/en/latest/howto/transactions.html; official text: “func (q *Queries) WithTx(tx *sql.Tx) *Queries { return &Queries{ db: tx, } }”
FAQ
- How does sqlc differ from an ORM?
- sqlc does not hide SQL. You write SQL and it generates Go structs and methods matching the result, so you keep full control and type safety without runtime reflection.
- How are query results typed?
- Each query annotation declares its shape, such as :one, :many or :exec, and sqlc infers column types from the schema to generate matching Go types.
- How do I handle migrations?
- sqlc reads the schema directory, so point it at your migrations or a schema dump. Keep the schema current, or generated types will not match the database.
Report a problem with this API docOpens a pre-filled issue on GitHub.