Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion sqlx-sqlite/src/connection/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,23 @@ pub(crate) fn describe(
let col_nullable = stmt.handle.column_nullable(col)?;
let exp_nullable = fallback_nullable.get(col).copied().and_then(identity);

nullable.push(exp_nullable.or(col_nullable));
// If the column has a known schema origin that says NOT NULL,
// trust that over the explain analysis which may lose NOT NULL
// constraints through ephemeral tables / sorters (e.g. ORDER BY).
// See: https://github.com/launchbadge/sqlx/issues/4147
let result_nullable = match (col_nullable, exp_nullable) {
// Schema says NOT NULL — trust it regardless of explain result
(Some(false), _) => Some(false),
// Schema doesn't know (e.g. expression column), use explain
(None, exp) => exp,
// Both agree or only schema has info
(col, None) => col,
// Schema says nullable, explain says not — be conservative, say nullable
(Some(true), Some(false)) => Some(true),
// Both say nullable
(Some(true), Some(true)) => Some(true),
};
nullable.push(result_nullable);

columns.push(SqliteColumn {
name: name.into(),
Expand Down
Loading