Skip to content

Comments

fix: SQLite AnyQueryResult::last_insert_id() always returns None#4178

Open
CairoLee wants to merge 1 commit intolaunchbadge:mainfrom
CairoLee:fix/sqlite-any-last-insert-id
Open

fix: SQLite AnyQueryResult::last_insert_id() always returns None#4178
CairoLee wants to merge 1 commit intolaunchbadge:mainfrom
CairoLee:fix/sqlite-any-last-insert-id

Conversation

@CairoLee
Copy link

Summary

AnyQueryResult::last_insert_id() always returns None when using the SQLite backend through the Any driver, even after a successful INSERT into a table with INTEGER PRIMARY KEY AUTOINCREMENT.

Root Cause

The map_result() function in sqlx-sqlite/src/any.rs hardcodes last_insert_id: None:

fn map_result(res: SqliteQueryResult) -> AnyQueryResult {
    AnyQueryResult {
        rows_affected: res.rows_affected(),
        last_insert_id: None, // <-- always None
    }
}

This is the function called by AnyConnectionBackend::fetch_many() for every query result.

Note that #3608 added a correct From<SqliteQueryResult> for AnyQueryResult implementation in query_result.rs, but the fetch_many code path still calls map_result() rather than using From, so that fix never takes effect.

Fix

Replace the manual construction in map_result() with a delegation to the existing From impl:

fn map_result(res: SqliteQueryResult) -> AnyQueryResult {
    AnyQueryResult::from(res)
}

Reproduction

sqlx::any::install_default_drivers();

let pool = AnyPool::connect("sqlite::memory:").await?;

sqlx::query("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL)")
    .execute(&pool)
    .await?;

let result = sqlx::query("INSERT INTO test (name) VALUES (?)")
    .bind("Alice")
    .execute(&pool)
    .await?;

// Before fix: None
// After fix:  Some(1)
println!("{:?}", result.last_insert_id());

Test Plan

  • Added regression test any_query_result_has_last_insert_id in tests/sqlite/any.rs
  • Test creates a temporary table, inserts two rows, and asserts that last_insert_id() returns Some(1) and Some(2) respectively

Closes #2982

…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
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.

AnyQueryResult does not map last_insert_id correct

1 participant