SQLx’s derive macros eliminate most of the repetitive trait implementation code that comes with a custom type system. With a single attribute you can generate row-to-struct mapping, SQL type declarations, parameter encoding, and column decoding — all from the shape of your Rust types. This page covers each macro and the attributes that control the generated code.Documentation 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.
All derive macros require the The
derive feature flag:macros feature (for query! / query_as!) implies derive, so you only need to add it explicitly if you are not using the query macros.#[derive(FromRow)]
FromRow maps a database row to a Rust struct. It is required by sqlx::query_as and sqlx::query_as! to deserialise query results.
Row::try_get for each field using the field’s name as the column name. All field types must implement Decode and Type for the target database.
Field attributes
#[sqlx(rename = "column_name")]
#[sqlx(rename = "column_name")]
Maps a struct field to a differently-named column.
#[sqlx(rename_all = "...")]
#[sqlx(rename_all = "...")]
Applied at the struct level, converts all field names to the given case before matching against column names. Useful when your database uses a different naming convention than your Rust code.Supported values:
snake_case, lowercase, UPPERCASE, camelCase, PascalCase, SCREAMING_SNAKE_CASE, kebab-case.Numbers are not treated as word boundaries.
foo1 in snake case stays foo1, not foo_1.#[sqlx(default)]
#[sqlx(default)]
When a column is missing from the query result, uses You can also place
Default::default() for the field value instead of returning an error.#[sqlx(default)] at the struct level when the struct itself implements Default. Any column absent from the result is taken from the struct’s Default implementation:#[sqlx(flatten)]
#[sqlx(flatten)]
Tells SQLx to call the field type’s own
FromRow implementation instead of calling try_get directly. Use this to embed one struct inside another when the columns all come from the same flat row.flatten is compatible with default.#[sqlx(skip)]
#[sqlx(skip)]
Always populates the field from
Default::default(), ignoring any column with the same name. Useful for fields that cannot or should not be decoded from SQL (for example, fields that hold non-SQL state like a Vec of related records).#[sqlx(try_from = "T")]
#[sqlx(try_from = "T")]
Decodes the column as type
T and then converts to the field’s type using TryFrom. Handy when the database type differs from the Rust type you want to store.#[sqlx(json)]
#[sqlx(json)]
Decodes a JSON or JSONB column and deserialises it using By default, the column must not be
serde::Deserialize. Requires the json feature.NULL. If the column is nullable and the JSON value itself is never null, use #[sqlx(json(nullable))]:Manual FromRow implementation
When the derive macro is not flexible enough, implement the trait yourself:
#[derive(Type)]
Type declares a SQL type for a Rust type, together with Encode and Decode implementations. It supports three modes: transparent newtypes, enumerations, and PostgreSQL composite types.
Transparent newtypes
#[sqlx(transparent)] makes a newtype delegate entirely to its inner field:
Type impl reports the same SQL type as i64 (e.g. BIGINT). Encode and Decode forward to i64.
Additional attributes for transparent types:
| Attribute | Effect |
|---|---|
#[sqlx(type_name = "...")] | Override the SQL type name instead of inferring it. PostgreSQL only. |
#[sqlx(no_pg_array)] | Skip generating PgHasArrayType. Required if the inner type does not implement PgHasArrayType (e.g. Vec<T>). |
Enum mapping
Enums can map to SQL integers or SQL strings depending on whether a#[repr(_)] attribute is present.
- Integer discriminants
- String variants
With The SQL type is the integer type matching the
#[repr(i32)] (or any integer repr), variants are stored and retrieved as their discriminant value:repr (e.g. INT for i32).PostgreSQL composite types
A struct with named fields derives a composite type (PostgreSQL only). Thetype_name attribute sets the PostgreSQL type name:
#[derive(Encode)]
Encode alone is rarely needed; #[derive(Type)] generates it together with Decode. Use #[derive(Encode)] when you need parameter encoding but not decoding, or when you want encoding behaviour independent of Type.
encode_by_ref to the inner String. For structs and enums, the same rules as #[derive(Type)] apply.
#[derive(Decode)]
Similarly, #[derive(Decode)] generates the Decode trait implementation on its own:
Summary
FromRow
Maps a database row to a struct. Required for
query_as. Supports renaming, defaults, flattening, skipping, type conversion, and JSON fields.Type
Declares a SQL type for a Rust type and generates
Encode + Decode. Supports transparent newtypes, integer or string enums, and PostgreSQL composites.Encode
Generates parameter binding only. Use when the type is write-only (query parameters) or when you need a custom
Type with generated encoding.Decode
Generates column decoding only. Use when the type is read-only (query results) or when you need a custom
Type with generated decoding.