Closed heroin-moose closed 2 years ago
However, this works (given e
implements Clone):
e.clone().insert(pool).await
I believe this is working as designed.
The query depends on the Example
because it needs to populate the query parameters. Since you're only borrowing it, the compiler can't guarantee that the &Example
lives as long as the query.
You can solve this by moving example into your insert
, or clone
ing e
, as you did.
You mean the query has the lifetime of pool?
Because raw query works, e.g. this compiles ok:
async fn explicit_insert(e: &Example, pool: &SqlitePool) -> sqlx::Result<()> {
sqlx::query("INSERT INTO example (id) VALUES ($1)")
.bind(&e.id)
.execute(pool)
.await?;
Ok(())
}
I still pass the id by reference but compiler is happy. Am I missing something obvious?
You mean the query has the lifetime of pool?
Not exactly. In your function, e
and pool
have different (unspecified) lifetimes.
ormlite::Model.insert
creates a ::sqlx::QueryAs
which takes the model's fields as borrowed arguments. Importantly, the binding happens asynchronously, because it happens inside the Model.insert
function. Which is why the compiler complains about &Example
. If you move it fn insert(e: Example, ...)
, or clone it, this issue should go away.
In your second post, you manually do the binding, thus the binding happens synchronously, and the problem goes away.
Thanks for the details!
Code:
Error: