Skip to content

SQLite query macros don't reject INSERTs that omit NOT NULL columns without defaults #4206

@pascalporedda

Description

@pascalporedda

Summary

With sqlx 0.8.6 and SQLite, a compile-time checked sqlx::query! INSERT that omits a NOT NULL / no-DEFAULT column still compiles. SQLite then rejects it only at runtime with NOT NULL constraint failed.

Reproduction

Repo: https://github.com/pascalporedda/sqlx-sqlite-not-null-repro

Schema:

CREATE TABLE session_group (
    prop_a TEXT NOT NULL,
    prop_b INTEGER NOT NULL,
    prop_c TEXT NOT NULL
);

Query that compiles:

sqlx::query!(
    r#"
    INSERT INTO session_group (prop_a, prop_b)
    VALUES (?, ?)
    "#,
    "test1",
    123_i64,
)

Commands:

cargo check
cargo run --quiet

Observed runtime output:

full insert rows_affected = 1
partial insert runtime result = Err(
    Database(
        SqliteError {
            code: 1299,
            message: "NOT NULL constraint failed: session_group.prop_c",
        },
    ),
)
persisted rows = [
    "ok:1:present",
]
row count = 1

Expected

I expected the compile-time checked query macro to reject the INSERT because prop_c is omitted and is NOT NULL with no default.

Actual

The query compiles and only fails when executed.

Notes

I looked through the 0.8.6 sources locally and it appears the SQLite describe path provides bind parameter count and result-column metadata, but not target-table write completeness. If this is the intended limit of checking for SQLite, a documentation note would help. If not, this looks like a missing compile-time verification case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions