As pointed out in the comments ever since SQLite 3.35 / Diesel 2.0, you can use .get_result()
with an SQLite backend as well by using the returning_clauses_for_sqlite_3_35
feature.
Lets say that we have a table called tournaments
CREATE TABLE tournaments (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
name VARCHAR NOT NULL
)
and we want to add a new tournament into it, we can get the inserted row with .get_result()
// Schema
// @generated automatically by Diesel CLI.
diesel::table! {
tournaments (id) {
id -> Integer,
name -> Text,
}
}
// Model
#[derive(Insertable)]
#[diesel(table_name = tournaments)]
pub struct NewTournament<'a> {
pub name: &'a str
}
fn new_tournament(name: &str, connection: diesel::sqlite::SqliteConnection) -> i32 {
println!("Adding new tournament \"{name}\"");
let new_tournament = NewTournament {name: name};
let result = diesel::insert_into(tournaments::table)
.values(new_tournament)
.get_result::<(i32, String)>(&mut connection).unwrap();
result.0