From 26a3f00f82b6ddec2bbdb3ecb0c29d6ff0da7472 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Fri, 25 Aug 2023 17:58:26 -0700 Subject: [PATCH] Work around bug in sqlx database example. --- examples/databases/src/sqlx.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/databases/src/sqlx.rs b/examples/databases/src/sqlx.rs index 0eb6a30f..c52cc2e7 100644 --- a/examples/databases/src/sqlx.rs +++ b/examples/databases/src/sqlx.rs @@ -24,12 +24,16 @@ struct Post { #[post("/", data = "")] async fn create(mut db: Connection, mut post: Json) -> Result>> { - 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::>() + .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)) }