sqlx_migrator icon indicating copy to clipboard operation
sqlx_migrator copied to clipboard

Using `raw_sql` gives lifetime error

Open SimenB opened this issue 1 year ago • 4 comments

Apply this diff

diff --git i/examples/postgres/migrations/m0005_reference_complex.rs w/examples/postgres/migrations/m0005_reference_complex.rs
index 7ed796a..72264a4 100644
--- i/examples/postgres/migrations/m0005_reference_complex.rs
+++ w/examples/postgres/migrations/m0005_reference_complex.rs
@@ -8,7 +8,7 @@ pub(crate) struct M0005Operation;
 #[async_trait::async_trait]
 impl Operation<Postgres> for M0005Operation {
     async fn up(&self, connection: &mut PgConnection) -> Result<(), Error> {
-        sqlx::query("INSERT INTO sample (id, name) VALUES (888, 'complex')")
+        sqlx::raw_sql("INSERT INTO sample (id, name) VALUES (888, 'complex')")
             .execute(connection)
             .await?;
         Ok(())
$ cargo run --example postgres --features="postgres cli"

error: implementation of `Executor` is not general enough
  --> examples/postgres/migrations/m0005_reference_complex.rs:10:76
   |
10 |       async fn up(&self, connection: &mut PgConnection) -> Result<(), Error> {
   |  ____________________________________________________________________________^
11 | |         sqlx::raw_sql("INSERT INTO sample (id, name) VALUES (888, 'complex')")
12 | |             .execute(connection)
13 | |             .await?;
14 | |         Ok(())
15 | |     }
   | |_____^ implementation of `Executor` is not general enough
   |
   = note: `Executor<'0>` would have to be implemented for the type `&mut PgConnection`, for any lifetime `'0`...
   = note: ...but `Executor<'1>` is actually implemented for the type `&'1 mut PgConnection`, for some specific lifetime `'1`

https://docs.rs/sqlx/0.8.2/sqlx/fn.raw_sql.html

SimenB avatar Nov 05 '24 10:11 SimenB

Hmm, doing connection.execute(sqlx::raw_sql(&sql)).await?; rather than sqlx::raw_sql(&sql).execute(connection).await?; seems to work. Any reason not to do that?

SimenB avatar Nov 05 '24 10:11 SimenB

sqlx::raw_sql(&sql).execute(connection).await?; is actually calling connection.execute(sqlx::raw_sql(&sql)).await?; internally. They are both same checking why it is actually failing. For now you can use your solution

iamsauravsharma avatar Nov 05 '24 10:11 iamsauravsharma

looks like this is upstream issue of sqlx i have created issue https://github.com/launchbadge/sqlx/issues/3591 at upstream

iamsauravsharma avatar Nov 05 '24 11:11 iamsauravsharma

Thanks!

SimenB avatar Nov 05 '24 13:11 SimenB

Closing this PR since upstream have solved main issue according to PR as well as it is not directly related to this project

iamsauravsharma avatar Jul 23 '25 02:07 iamsauravsharma