Applies when building or reviewing SQLx database code in Rust. Covers compile-time queries, connection pooling, transactions, migrations, and testing.
sqlx::query! and query_as! for compile-time SQL checking. Catches typos, type mismatches, invalid SQL before runtime.cargo sqlx prepare locally, commit .sqlx/, SQLX_OFFLINE=true in CI.query_scalar! for single-value queries (COUNT, EXISTS).max_connections, acquire_timeout. Never per-request.Option<T>. Missing Option = runtime error.fetch_one exactly one. fetch_optional zero or one. small sets. streaming for large.fetch_allfetchpool.begin(), &mut *tx as executor, tx.commit() explicitly. Drop = auto-rollback.sqlx::migrate!() at startup or sqlx-cli.#[derive(sqlx::Type)] for enum mapping. #[sqlx(type_name = "my_enum")].QueryBuilder with push_values for batch inserts. Not looped inserts.sqlx::Error variants explicitly. RowNotFound (404), unique violation (409), others (500).#[sqlx::test] creates temp DB, runs migrations, provides pool, drops after.format! to build SQL. Always bind parameters.