Skip to content

Commit

Permalink
Work around bug in sqlx database example.
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioBenitez committed Aug 26, 2023
1 parent 695cf3a commit 26a3f00
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions examples/databases/src/sqlx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down

0 comments on commit 26a3f00

Please sign in to comment.