SQLx’s macro family verifies your SQL queries against a live database (or a cached offline snapshot) at compile time. The compiler rejects queries with syntax errors, unknown tables or columns, and type mismatches before a single binary is shipped. All macros in this family require theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/launchbadge/sqlx/llms.txt
Use this file to discover all available pages before exploring further.
macros feature flag, and they all read the database URL from the DATABASE_URL environment variable at compile time (or from the variable specified in sqlx.toml).
Enable compile-time checking by adding You must have a running database accessible at
macros to your feature list:DATABASE_URL during cargo build, or use cargo sqlx prepare to generate an offline snapshot.query!
Map that yields anonymous record structs — one field per selected column. Field names are taken directly from the column names in the result set.
The return type is unnameable (the struct is anonymous), so it cannot be stored in a named variable with an explicit type annotation. Use query_as! when you need a named, reusable struct.
Bind parameters
Bind values are passed as positional arguments after the SQL string. Their Rust types must be compatible with the corresponding SQL parameter types; mismatches are compile errors.Type override syntax
When the macro infers an unexpected type, you can coerce it with anas cast inside the argument list:
as syntax is also required for non-'static references and custom Type implementations that the macro cannot fully infer.
query_as!
query!, but maps each row into a named struct $out_type instead of an anonymous record. The struct must have a field for each selected column (matched by name), and each field type must be compatible with the corresponding SQL column type.
query_as! does not require #[derive(FromRow)] on the struct — it generates the mapping code at compile time and checks column names and types directly.
query_scalar!
sqlx::query_scalar. Extracts the first column of each row into the inferred Rust scalar type.
Type override syntax (detailed)
All three query macros support the same type override syntax. Use it to:- Resolve ambiguity when both
chronoandtimeare enabled (also configurable viasqlx.toml) - Pass slice references (arrays) to Postgres
ANY($1)clauses - Use custom newtype wrappers
&expr as &Type is the most common form:
migrate!
Migrator that can apply them at runtime. Requires the migrate feature in addition to macros.
migrate!() looks for migration files in a migrations/ directory relative to the crate root. Pass a string literal to use a different path.
<VERSION>_<description>.sql for simple (non-reversible) migrations, or <VERSION>_<description>.up.sql / <VERSION>_<description>.down.sql for reversible ones. The migration table defaults to _sqlx_migrations and can be renamed in sqlx.toml.
#[sqlx::test]
macros and migrate features.
Basic usage
Annotate an async test function with#[sqlx::test]. SQLx injects a PgPool (or the pool type matching your enabled database feature) that is pre-configured for isolation.
Automatic migrations
If your project usesmigrate!(), passing a Migrator to the attribute runs migrations before each test:
What it does under the hood
- Creates a new database by cloning a template (Postgres) or using an in-memory database (SQLite).
- Optionally applies migrations to bring the schema up to date.
- Runs the test body with a pool connected to that database.
- Drops the database when the test completes (pass or fail).