pub struct Config {
    pub preferred_crates: PreferredCrates,
    pub type_overrides: BTreeMap<SqlType, RustType>,
    pub table_overrides: BTreeMap<TableName, BTreeMap<ColumnName, RustType>>,
}Expand description
Configuration for the query!() family of macros.
See also common::Config for renaming DATABASE_URL.
Fields§
§preferred_crates: PreferredCratesSpecify which crates’ types to use when types from multiple crates apply.
See PreferredCrates for details.
type_overrides: BTreeMap<SqlType, RustType>Specify global overrides for mapping SQL type names to Rust type names.
Default type mappings are defined by the database driver.
Refer to the sqlx::types module for details.
§Note: Case-Sensitive
Currently, the case of the type name MUST match the name SQLx knows it by. Built-in types are spelled in all-uppercase to match SQL convention.
However, user-created types in Postgres are all-lowercase unless quoted.
§Note: Orthogonal to Nullability
These overrides do not affect whether query!() decides to wrap a column in Option<_>
or not. They only override the inner type used.
§Note: Schema Qualification (Postgres)
Type names may be schema-qualified in Postgres. If so, the schema should be part
of the type string, e.g. 'foo.bar' to reference type bar in schema foo.
The schema and/or type name may additionally be quoted in the string for a quoted identifier (see next section).
Schema qualification should not be used for types in the search path.
§Note: Quoted Identifiers (Postgres)
Type names using quoted identifiers in Postgres must also be specified with quotes here.
Note, however, that the TOML format parses way the outer pair of quotes,
so for quoted names in Postgres, double-quoting is necessary,
e.g. '"Foo"' for SQL type "Foo".
To reference a schema-qualified type with a quoted name, use double-quotes after the
dot, e.g. 'foo."Bar"' to reference type "Bar" of schema foo, and vice versa for
quoted schema names.
We recommend wrapping all type names in single quotes, as shown below, to avoid confusion.
MySQL/MariaDB and SQLite do not support custom types, so quoting type names should never be necessary.
§Example: Custom Wrapper Types
Does SQLx not support a type that you need? Do you want additional semantics not implemented on the built-in types? You can create a custom wrapper, or use an external crate.
§sqlx.toml
[macros.type-overrides]
# Override a built-in type
'UUID' = "crate::types::MyUuid"
# Support an external or custom wrapper type (e.g. from the `isn` Postgres extension)
# (NOTE: FOR DOCUMENTATION PURPOSES ONLY; THIS CRATE/TYPE DOES NOT EXIST AS OF WRITING)
'isbn13' = "isn_rs::sqlx::ISBN13"§Example: Custom Types in Postgres
If you have a custom type in Postgres that you want to map without needing to use
the type override syntax in sqlx::query!() every time, you can specify a global
override here.
For example, a custom enum type foo:
§Migration or Setup SQL (e.g. migrations/0_setup.sql)
CREATE TYPE foo AS ENUM ('Bar', 'Baz');§src/types.rs
#[derive(sqlx::Type)]
pub enum Foo {
    Bar,
    Baz
}If you’re not using PascalCase in your enum variants then you’ll want to use
#[sqlx(rename_all = "<strategy>")] on your enum.
See [Type][crate::type::Type] for details.
§sqlx.toml
[macros.type-overrides]
# Map SQL type `foo` to `crate::types::Foo`
'foo' = "crate::types::Foo"§Example: Schema-Qualified Types
(See Note section above for details.)
[macros.type-overrides]
# Map SQL type `foo.foo` to `crate::types::Foo`
'foo.foo' = "crate::types::Foo"§Example: Quoted Identifiers
If a type or schema uses quoted identifiers, it must be wrapped in quotes twice for SQLx to know the difference:
[macros.type-overrides]
# `"Foo"` in SQLx
'"Foo"' = "crate::types::Foo"
# **NOT** `"Foo"` in SQLx (parses as just `Foo`)
"Foo" = "crate::types::Foo"
# Schema-qualified
'"foo".foo' = "crate::types::Foo"
'foo."Foo"' = "crate::types::Foo"
'"foo"."Foo"' = "crate::types::Foo"(See Note section above for details.)
table_overrides: BTreeMap<TableName, BTreeMap<ColumnName, RustType>>Specify per-table and per-column overrides for mapping SQL types to Rust types.
Default type mappings are defined by the database driver.
Refer to the sqlx::types module for details.
The supported syntax is similar to type_overrides,
(with the same caveat for quoted names!) but column names must be qualified
by a separately quoted table name, which may optionally be schema-qualified.
Multiple columns for the same SQL table may be written in the same table in TOML (see examples below).
§Note: Orthogonal to Nullability
These overrides do not affect whether query!() decides to wrap a column in Option<_>
or not. They only override the inner type used.
§Note: Schema Qualification
Table names may be schema-qualified. If so, the schema should be part
of the table name string, e.g. 'foo.bar' to reference table bar in schema foo.
The schema and/or type name may additionally be quoted in the string for a quoted identifier (see next section).
Postgres users: schema qualification should not be used for tables in the search path.
§Note: Quoted Identifiers
Schema, table, or column names using quoted identifiers (MySQL, Postgres, SQLite) in SQL must also be specified with quotes here.
Postgres and SQLite use double-quotes ("Foo") while MySQL uses backticks (\Foo`).
Note, however, that the TOML format parses way the outer pair of quotes,
so for quoted names in Postgres, double-quoting is necessary,
e.g. '"Foo"' for SQL name "Foo".
To reference a schema-qualified table with a quoted name, use the appropriate quotation
characters after the dot, e.g. 'foo."Bar"' to reference table "Bar" of schema foo,
and vice versa for quoted schema names.
We recommend wrapping all table and column names in single quotes, as shown below, to avoid confusion.
§Example
§sqlx.toml
[macros.table-overrides.'foo']
# Map column `bar` of table `foo` to Rust type `crate::types::Foo`:
'bar' = "crate::types::Bar"
# Quoted column name
# Note: same quoting requirements as `macros.type_overrides`
'"Bar"' = "crate::types::Bar"
# Note: will NOT work (parses as `Bar`)
# "Bar" = "crate::types::Bar"
# Table name may be quoted (note the wrapping single-quotes)
[macros.table-overrides.'"Foo"']
'bar' = "crate::types::Bar"
'"Bar"' = "crate::types::Bar"
# Table name may also be schema-qualified.
# Note how the dot is inside the quotes.
[macros.table-overrides.'my_schema.my_table']
'my_column' = "crate::types::MyType"
# Quoted schema, table, and column names
[macros.table-overrides.'"My Schema"."My Table"']
'"My Column"' = "crate::types::MyType"Implementations§
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Config
impl RefUnwindSafe for Config
impl Send for Config
impl Sync for Config
impl Unpin for Config
impl UnwindSafe for Config
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
 
impl<T> BorrowMut<T> for Twhere
    T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
 
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
 
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
 
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
 
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
 
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
 
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
 
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more