Skip to content

Add last_insert_rowid method to SqliteConnection and RawConnection#4977

Open
Alexi24601 wants to merge 3 commits intodiesel-rs:mainfrom
Alexi24601:sqlite3_last_insert_rowid
Open

Add last_insert_rowid method to SqliteConnection and RawConnection#4977
Alexi24601 wants to merge 3 commits intodiesel-rs:mainfrom
Alexi24601:sqlite3_last_insert_rowid

Conversation

@Alexi24601
Copy link

Exposes SQLite's sqlite3_last_insert_rowid() through SqliteConnection, following the same pattern as the existing rows_affected_by_last_query.

Returns Option<i64>None if no INSERT has been performed on the connection, Some(rowid) otherwise.

Example

use diesel::prelude::*;
use diesel::connection::SimpleConnection;
use diesel::sqlite::SqliteConnection;

let conn = &mut SqliteConnection::establish(":memory:").unwrap();
conn.batch_execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL)").unwrap();

assert_eq!(conn.last_insert_rowid(), None);

conn.batch_execute("INSERT INTO users (name) VALUES ('Sean')").unwrap();
assert_eq!(conn.last_insert_rowid(), Some(1));

conn.batch_execute("INSERT INTO users (name) VALUES ('Tess')").unwrap();
assert_eq!(conn.last_insert_rowid(), Some(2));

Notes

  • Only affected by successful INSERTs into rowid tables
  • Not changed by failed INSERTs, UPDATEs, or DELETEs
  • Scoped per-connection

@LucaCappelletti94 made me aware of the need to do this, so i tried my hand at this. It is still among the very first pr i do, i am still learning Rust and how to properly use git.

@weiznich weiznich requested a review from a team February 15, 2026 06:47
@Alexi24601 Alexi24601 marked this pull request as ready for review February 15, 2026 07:46
@LucaCappelletti94
Copy link
Contributor

LucaCappelletti94 commented Feb 16, 2026

@weiznich LGTM, the only thing that could be improved at all could maybe be to replace the INSERT/DELETE raw SQL in the test submodules with actual ORM calls if I really were to be extra nitpicky

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.

2 participants