Repository: launchbadge/sqlx

Issue #641: How do I use query().map in 0.4?
Created: 2020-08-17T16:24:05Z
Both in 0.4.0-beta.1 and on master I can't figure out how to use query().map, this works
use std::env;
use sqlx::postgres::{PgPool, PgRow};
use sqlx::Row;

struct Book {
    id: i32,
    title: String,
}

#[async_std::main]
async fn main() -> Result<(), sqlx::Error> {
    let pool = PgPool::connect(&env::var("DATABASE_URL").expect("env error!")).await?;

    sqlx::query("SELECT id, title FROM books")
        .try_map(|row: PgRow| {
            Ok(Book {
                id: row.get("id"),
                title: row.get("title"),
            })
        })
        .fetch_one(&pool)
        .await
        .expect("fetch error!");

    Ok(())
}
but this doesn't
use sqlx::postgres::{PgPool, PgRow};
use sqlx::Row;
use std::env;

struct Book {
    id: i32,
    title: String,
}

#[async_std::main]
async fn main() -> Result<(), sqlx::Error> {
    let pool = PgPool::connect(&env::var("DATABASE_URL").expect("env error!")).await?;

    sqlx::query("SELECT id, title FROM books")
        .map(|row: PgRow| Book {
            id: row.get("id"),
            title: row.get("title"),
        })
        .fetch_one(&pool)
        .await
        .expect("fetch error!");

    Ok(())
}
error[E0599]: no method named `fetch_one` found for struct `sqlx_core::query::Map<'_, sqlx_core::postgres::database::Postgres, impl sqlx_core::query::TryMapRow<sqlx_core::postgres::database::Postgres>, sqlx_core::postgres::arguments::PgArguments>` in the current scope
  --> src/main.rs:21:10
   |
21 |         .fetch_one(&pool)
   |          ^^^^^^^^^ method not found in `sqlx_core::query::Map<'_, sqlx_core::postgres::database::Postgres, impl sqlx_core::query::TryMapRow<sqlx_core::postgres::database::Postgres>, sqlx_core::postgres::arguments::PgArguments>`
   |
   = note: the method `fetch_one` exists but the following trait bounds were not satisfied:
           `<impl sqlx_core::query::TryMapRow<sqlx_core::postgres::database::Postgres> as std::ops::FnOnce<(sqlx_core::postgres::row::PgRow,)>>::Output = std::result::Result<_, sqlx_core::error::Error>`
           `impl sqlx_core::query::TryMapRow<sqlx_core::postgres::database::Postgres>: std::ops::Fn<(sqlx_core::postgres::row::PgRow,)>`
Both works fine in 0.3.5

Duplicate issue #715: code compiles in 0.3 and doesn't in 0.4.0-beta.1
Created: 2020-09-29T19:44:03Z
rust: 1.46.0 (ubuntu: 20.04 LTS)
This code extract compiles fine with 0.3, but complains with 0.4.0-beta.1:
#[derive(Deserialize, Serialize, FromRow)]
pub struct World {
    pub id: Option<i32>, // Only populated when in database
    pub name: String,
    pub username: String
}
impl World {
    pub async fn new(request: World, pool: &PgPool) -> Result<World> {
        let mut tx = pool.begin().await?;
        let world = sqlx::query("INSERT INTO worlds (name, username) VALUES ($1, $2) RETURNING id, name, username")
            .bind(&request.name)
            .bind(&request.username)
            .map(|row: PgRow| {
                World {
                    id: row.get(0),
                    name: row.get(1),
                    username: row.get(2)
                }
            })
            .fetch_one(&mut tx)
            .await?;
        tx.commit().await?;
        Ok(world)
    }
}

The error message is:
error[E0599]: no method named `fetch_one` found for struct `sqlx::query::Map<'_, sqlx::Postgres, impl sqlx::query::TryMapRow<sqlx::Postgres>, sqlx::postgres::PgArguments>` in the current scope
  --> src/model/worlds.rs:26:14
   |
26 |             .fetch_one(&mut tx)
   |              ^^^^^^^^^ method not found in `sqlx::query::Map<'_, sqlx::Postgres, impl sqlx::query::TryMapRow<sqlx::Postgres>, sqlx::postgres::PgArguments>`
   |
   = note: the method `fetch_one` exists but the following trait bounds were not satisfied:
           `<impl sqlx::query::TryMapRow<sqlx::Postgres> as std::ops::FnOnce<(sqlx::postgres::PgRow,)>>::Output = std::result::Result<_, sqlx::Error>`
           `impl sqlx::query::TryMapRow<sqlx::Postgres>: std::ops::Fn<(sqlx::postgres::PgRow,)>`

I tried a few different combinations of enabling/disabling features, but it didn't seem to make any difference.

Closing comment (2020-09-30T07:31:25Z): Looks like a duplicate of #641