TLDR
How can I get the index of a newly-inserted row, using the type-checked sqlx
macros?
Details
Comparing sqlx
queries with the form
let a = sqlx::query!("INSERT INTO ..." , ...);
let b = sqlx::query!("INSERT INTO ... RETURNING id", ...);
that is to say, the first merely inserts a row into the table, while the second also returns the new now's index, the types of a
and b
, as reported by rust-analyzer, are
a: Query<'_, Postgres, PgArguments>
b: Map<'_, Postgres, impl FnMut(PgRow) -> Result<Record, Error>, PgArguments>
respectively.
The Map
seems to be this one: Map in sqlx::query - Rust, whose docs state that it
Has most of the same methods as
Query
but the return types are changed to reflect the mapping. However, there is no equivalent ofQuery::execute
as it doesn’t make sense to map the result type and then ignore it.
From which I infer that I should be using query_as!
instead of query!
. Fair enough, but query_as!
needs a struct specifying the returned type. I this case it should be the type of the index, which is i64
, but query_as!
rejects it for not being 'a struct, variant or union type'.