From 3aca20325feb12624d55e61c29149da9dd1708d9 Mon Sep 17 00:00:00 2001 From: ChinhLee <76194645+chinhkrb113@users.noreply.github.com> Date: Sun, 31 May 2026 20:36:51 +0700 Subject: [PATCH] =?UTF-8?q?fix:=20resolve=20#2642=20=E2=80=94=20Query=20ma?= =?UTF-8?q?cro=20documentation=20understates=20lack=20of=20nullability=20c?= =?UTF-8?q?hecking=20on=20bind=20parameters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #2642 Signed-off-by: ChinhLee <76194645+chinhkrb113@users.noreply.github.com> --- src/macros/mod.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/macros/mod.rs b/src/macros/mod.rs index 0db6f0c2e7..42acb9117c 100644 --- a/src/macros/mod.rs +++ b/src/macros/mod.rs @@ -93,6 +93,28 @@ /// [configuration guide]: crate::_config::macros::Config /// [reference `sqlx.toml`]: crate::_config::_reference /// +/// ## Nullability of Bind Parameters +/// The `query!` family of macros checks the *types* of bind parameters against the +/// database's inferred parameter types, but they do **not** check whether a bound +/// value's nullability is compatible with the target column's `NOT NULL` constraint. +/// +/// Databases generally report all bind parameters as nullable, and the macro has no +/// way to associate a parameter with a specific column constraint at compile time. +/// As a result, passing a `None`/`NULL` value for a non-nullable column compiles +/// successfully and only fails at runtime with a database constraint violation: +/// +/// ```rust,ignore +/// // `name` is declared `NOT NULL`, but this still compiles; +/// // it only fails at runtime when executed: +/// let name: Option = None; +/// sqlx::query!("insert into accounts (name) values (?)", name) +/// .execute(&mut conn) +/// .await?; +/// ``` +/// +/// Null-checking is only performed on the *output* columns of a query, not on the +/// *input* bind parameters. +/// /// ## Query Arguments /// Like `println!()` and the other formatting macros, you can add bind parameters to your SQL /// and this macro will typecheck passed arguments and error on missing ones: