Skip to content

fix(sqlite): correctly infer nullability for INTEGER PRIMARY KEY in RETURNING#4323

Open
at264939-ctrl wants to merge 2 commits into
transact-rs:mainfrom
at264939-ctrl:fix/sqlite-returning-rowid-nullability
Open

fix(sqlite): correctly infer nullability for INTEGER PRIMARY KEY in RETURNING#4323
at264939-ctrl wants to merge 2 commits into
transact-rs:mainfrom
at264939-ctrl:fix/sqlite-returning-rowid-nullability

Conversation

@at264939-ctrl

Copy link
Copy Markdown

fixes #4322. This is a fix for a regression introduced in 0.9.0.

After upgrading to sqlx 0.9.0, many users noticed that INSERT ... RETURNING queries on INTEGER PRIMARY KEY columns started returning Option instead of T, which broke a lot of code that expects a simple i64. I traced this down to commit 69ee0df from PR #4088.

The issue is that in sqlx-sqlite/src/statement/handle.rs, column_nullable was changed to return None for rowid aliases. This forces the logic to fall back to the bytecode simulation in explain.rs, which relies on PRAGMA table_info. In SQLite, this pragma returns notnull=0 for INTEGER PRIMARY KEY columns because they can be passed as NULL during an insert. However, in any actual result set, these columns are never NULL.

To fix this, I updated root_block_columns in explain.rs to include the pk column from table_info so the VM simulation can properly identify these rowid aliases and mark them as non-nullable. I also updated handle.rs to return Some(false) instead of None for these cases to provide a more accurate initial inference. Finally, I adjusted a few existing tests in explain.rs that were previously asserting these columns as nullable. I've verified the fix with integration tests and it correctly restores the expected i64 behavior

@at264939-ctrl

Copy link
Copy Markdown
Author

Is there no one here for review?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlite INSERT ... RETURNING queries incorrectly return nullable

1 participant