Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlx lost transaction support starting version 0.7.0 #2699

Open
goodjius opened this issue Aug 19, 2023 · 6 comments · May be fixed by #3311
Open

sqlx lost transaction support starting version 0.7.0 #2699

goodjius opened this issue Aug 19, 2023 · 6 comments · May be fixed by #3311
Labels

Comments

@goodjius
Copy link

Bug Description

Get error: .fetch_one(&mut tx)
| --------- ^^^^^^^ the trait Executor<'_> is not implemented for &mut Transaction<'_, Sqlite>.
This is no problem with sqlx 0.6.x.
Compiler start to complain starting 0.7.0

Minimal Reproduction

        let mut tx = pool.begin().await?;
        let res = sqlx::query("DELETE FROM \"testcases\" WHERE id = $1")
            .bind(id)
            .execute(&mut tx)
            .await?
            .rows_affected();

        tx.commit().await?;

Info

  • SQLx version: 0.7.x
  • SQLx features enabled: sqlite
  • Database server and version: SQLite 3.39.5
  • Operating system: Mac OS
  • rustc --version: 1.71.0
@goodjius goodjius added the bug label Aug 19, 2023
@dragonnn
Copy link
Contributor

Please read the 0.7 changelog and look at the current examples. This is not a bug, just a breaking change how transactions are used

@dvush
Copy link

dvush commented Sep 12, 2023

@goodjius .
I was struggling with the same issue but exact advice from the changelog did not help, also I did not find example of the transaction for sqlite.

Here is the relevant part of the changelog mentioned by @dragonnn

The Executor impls for Transaction and PoolConnection have been deleted because they cannot exist in the new crate architecture without rewriting the Executor trait entirely.
To fix this breakage, simply add a dereference where an impl Executor is expected, as they both dereference to the inner connection type which will still implement it:
&mut transaction -> &mut *transaction
&mut connection -> &mut *connection

What helped was using as_mut. (although I am using connection.transaction)

My example

        self.conn.transaction(|tx| Box::pin(async move {
            sqlx::query(
                r#"...
            "#,
            ).bind(...)
             .execute(tx.as_mut())
             .await?;


            Ok::<_, eyre::Error>(())
        })).await?;

In your example this would be:

        let mut tx = pool.begin().await?;
        let res = sqlx::query("DELETE FROM \"testcases\" WHERE id = $1")
            .bind(id)
            .execute(tx.as_mut()) // <-- here
            .await?
            .rows_affected();

        tx.commit().await?;

@Lachstec
Copy link

Hi, sorry for bumping this old issue. I stumbled across the same problem, I wanted to use a transaction in a personal project and I wasn't able to get it to work at first. The examples in this repo were able to help me and I got it working, but I think it would be nice to add a short example on how to use Transaction as an executor to the docs.

Is that a good idea? If yes, I would submit a PR 😄.

@fosskers
Copy link

@Lachstec Yes please do it.

@Lachstec
Copy link

@fosskers Okay, I will get to it if I find some time today.

@Lachstec Lachstec linked a pull request Jun 25, 2024 that will close this issue
@Lachstec
Copy link

@fosskers I've submitted a PR where I added a Snippet inspired by the one in this issue, hope that it is fine like that :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants