fix: SQLite AnyQueryResult::last_insert_id() always returns None#4178
Open
CairoLee wants to merge 1 commit intolaunchbadge:mainfrom
Open
fix: SQLite AnyQueryResult::last_insert_id() always returns None#4178CairoLee wants to merge 1 commit intolaunchbadge:mainfrom
AnyQueryResult::last_insert_id() always returns None#4178CairoLee wants to merge 1 commit intolaunchbadge:mainfrom
Conversation
…sert_id` The `map_result()` function in `sqlx-sqlite/src/any.rs` was hardcoding `last_insert_id: None`, discarding the actual `last_insert_rowid` from `SqliteQueryResult`. Meanwhile, a correct `From<SqliteQueryResult>` implementation already existed in `query_result.rs` (added in launchbadge#3608) but was never used by the actual query execution path. This commit replaces the manual construction in `map_result()` with a delegation to `AnyQueryResult::from()`, which properly maps the SQLite `last_insert_rowid` value. A regression test is added to verify that `AnyQueryResult::last_insert_id()` returns the correct value after INSERT operations on SQLite. Closes launchbadge#2982
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
AnyQueryResult::last_insert_id()always returnsNonewhen using the SQLite backend through theAnydriver, even after a successfulINSERTinto a table withINTEGER PRIMARY KEY AUTOINCREMENT.Root Cause
The
map_result()function insqlx-sqlite/src/any.rshardcodeslast_insert_id: None:This is the function called by
AnyConnectionBackend::fetch_many()for every query result.Note that #3608 added a correct
From<SqliteQueryResult> for AnyQueryResultimplementation inquery_result.rs, but thefetch_manycode path still callsmap_result()rather than usingFrom, so that fix never takes effect.Fix
Replace the manual construction in
map_result()with a delegation to the existingFromimpl:Reproduction
Test Plan
any_query_result_has_last_insert_idintests/sqlite/any.rslast_insert_id()returnsSome(1)andSome(2)respectivelyCloses #2982