Work around bug in sqlx database example.

This commit is contained in:
Sergio Benitez 2023-08-25 17:58:26 -07:00
parent 695cf3aab1
commit 26a3f00f82
1 changed files with 9 additions and 5 deletions

View File

@ -24,12 +24,16 @@ struct Post {
#[post("/", data = "<post>")]
async fn create(mut db: Connection<Db>, mut post: Json<Post>) -> Result<Created<Json<Post>>> {
let query = sqlx::query! {
"INSERT INTO posts (title, text) VALUES (?, ?) RETURNING id",
post.title, post.text
};
// NOTE: sqlx#2543, sqlx#1648 mean we can't use the pithier `fetch_one()`.
let results = sqlx::query!(
"INSERT INTO posts (title, text) VALUES (?, ?) RETURNING id",
post.title, post.text
)
.fetch(&mut **db)
.try_collect::<Vec<_>>()
.await?;
post.id = Some(query.fetch_one(&mut **db).await?.id);
post.id = Some(results.first().expect("returning results").id);
Ok(Created::new("/").body(post))
}