launchbadge / sqlx

🧰 The Rust SQL Toolkit. An async, pure Rust SQL crate featuring compile-time checked queries without a DSL. Supports PostgreSQL, MySQL, and SQLite.
Apache License 2.0
13.15k stars 1.24k forks source link

If I insert an `Option<sqlx::types::Json<Struct>>` sqlx writes the text `null` in DB instead of mark that field as `NULL` #3294

Closed frederikhors closed 3 months ago

frederikhors commented 3 months ago

I thought the problem was me and my code and so I tried to reproduce the problem with your code in examples.

I only made minor changes by adding an Option<> to simulate my real problem.

- async fn add_person(pool: &PgPool, person: Person) -> anyhow::Result<i64> {
+ async fn add_person(pool: &PgPool, person: Option<Person>) -> anyhow::Result<i64> {

and in migrations:

- person JSONB NOT NULL
+ person JSONB

If I run it it is saved in the DB:

image

and instead I expect this:

image

Where am I doing wrong?

Minimal Reproduction

https://github.com/frederikhors/iss-sqlx-option-null

Info

I think we can also close these issues after this one (maybe):

  1. https://github.com/launchbadge/sqlx/issues/2438

  2. https://github.com/launchbadge/sqlx/issues/2849

frederikhors commented 3 months ago

My fault: https://users.rust-lang.org/t/if-i-insert-an-option-sqlx-json-struct-sqlx-writes-the-text-null-in-db-instead-of-mark-that-field-as-null/113078/2