Skip to content

Commit 26a3f00

Browse files
committed
Work around bug in sqlx database example.
1 parent 695cf3a commit 26a3f00

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

examples/databases/src/sqlx.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ struct Post {
2424

2525
#[post("/", data = "<post>")]
2626
async fn create(mut db: Connection<Db>, mut post: Json<Post>) -> Result<Created<Json<Post>>> {
27-
let query = sqlx::query! {
28-
"INSERT INTO posts (title, text) VALUES (?, ?) RETURNING id",
29-
post.title, post.text
30-
};
27+
// NOTE: sqlx#2543, sqlx#1648 mean we can't use the pithier `fetch_one()`.
28+
let results = sqlx::query!(
29+
"INSERT INTO posts (title, text) VALUES (?, ?) RETURNING id",
30+
post.title, post.text
31+
)
32+
.fetch(&mut **db)
33+
.try_collect::<Vec<_>>()
34+
.await?;
3135

32-
post.id = Some(query.fetch_one(&mut **db).await?.id);
36+
post.id = Some(results.first().expect("returning results").id);
3337
Ok(Created::new("/").body(post))
3438
}
3539

0 commit comments

Comments
 (0)