0

I'm using sqlx.

I'm stuck because I can't use the PgExecutor multiple time in the same method.

I would like your help to better understand what to use here because I read all the docs and all the issues but cannot find a way.

As you can see in my code I have methods that I can use both within a transaction or not.

Hence I tried to use impl sqlx::PgExecutor<'_> as argument and it worked.

But now I need to use it multiple times in the same method, like this:

pub async fn get_team_by_id(
    db: impl sqlx::PgExecutor<'_>,
    id: &str,
) -> Result<Team, String> {
    let mut query = sqlx::QueryBuilder::new("SELECT * FROM teams WHERE id = ");

    query.push_bind(id);

    let obj = query.build_query_as::<Team>().fetch_one(db).await.unwrap();

    // This is the new line (db is used again here)
    let coach = get_team_coach(db, &obj.coach_id).await?;

    dbg!(coach);

    Ok(obj)
}

The example is running here: https://www.rustexplorer.com/b/a7yv5f

The error is:

error[E0382]: use of moved value: `db`
   --> src/main.rs:121:40
    |
112 |             db: impl sqlx::PgExecutor<'_>,
    |             -- move occurs because `db` has type `impl sqlx::PgExecutor<'_>`, which does not implement the `Copy` trait
...
119 |             let obj = query.build_query_as::<Team>().fetch_one(db).await.unwrap();
    |                                                                -- value moved here
120 |
121 |             let coach = get_team_coach(db, &obj.coach_id).await?;
    |                                        ^^ value used here after move
    |
help: consider further restricting this bound
    |
112 |             db: impl sqlx::PgExecutor<'_> + std::marker::Copy,
    |                                           +++++++++++++++++++

I know they are working on a 0.7 version. Is it possible with 0.7 to use Executor multiple times?

Fred Hors
  • 3,258
  • 3
  • 25
  • 71

0 Answers0