-
I'm trying to dynamically construct Postgres query parameters. The Postgres params type is I tried this: async fn insert_multirow_once(DbConn(conn): DbConn) -> Result<impl IntoResponse, StatusCode> {
let statement = "INSERT INTO foo (a, b) VALUES ($1, $2), ($3, $4)";
let mut params = Vec::<Box<dyn ToSql + Sync>>::with_capacity(4);
for i in 1..=2 {
params.push(Box::new(i));
params.push(Box::new("s".to_owned()));
}
// from https://github.com/sfackler/rust-postgres/issues/712
let params = params.iter().map(|x| x.as_ref()).collect::<Vec<_>>();
conn.execute(statement, ¶ms)
.await
.map_err(internal_error)?;
Ok(StatusCode::OK)
} The function by itself is ok but it doesn't work as a handler. The error is I searched the issues for Full example here, based on the tokio-postgres example |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hm I'm not sure. Try asking the tokio-postgres maintainers. |
Beta Was this translation helpful? Give feedback.
-
I got it working. I had read the solution but forgot to actually cast. Changed this: let mut params = Vec::<Box<dyn ToSql + Sync + Send>>::with_capacity(4); // add Send and this let params = params
.iter()
.map(|x| x.as_ref() as &(dyn ToSql + Sync)) // need to cast
.collect::<Vec<_>>(); and now it works. Thanks for Axum! |
Beta Was this translation helpful? Give feedback.
I got it working. I had read the solution but forgot to actually cast.
Changed this:
and this
and now it works.
Thanks for Axum!