overdrivenpotato / bb8-diesel

bb8 connection manager for Diesel
MIT License
21 stars 12 forks source link

usage example not runnable #9

Open ni2scmn opened 2 years ago

ni2scmn commented 2 years ago

Hi,

I tried to run the usage example but I do not get it compiled.

` use diesel::pg::PgConnection; use diesel::prelude::*;

table! { users (id) { id -> Integer, name -> Text, } }

[derive(AsChangeset, Identifiable, Queryable, Eq, PartialEq)]

[diesel(table_name = users)]

pub struct User { pub id: i32, pub name: String, }

[tokio::main]

async fn main() { use users::dsl;

let manager = bb8_diesel::DieselConnectionManager::<PgConnection>::new("localhost:1234");
let pool = bb8::Pool::builder().build(manager).await.unwrap();
let mut conn = pool.get().await.unwrap();

// Insert
let _ = diesel::insert_into(dsl::users)
    .values((dsl::id.eq(0), dsl::name.eq("Jim")))
    .execute(&mut *conn)
    .unwrap();

// Load
let _ = dsl::users.load::<User>(&mut *conn).unwrap();

// Update
let _ = diesel::update(dsl::users)
    .filter(dsl::id.eq(0))
    .set(dsl::name.eq("Jim, But Different"))
    .execute(&mut *conn)
    .unwrap();

// Update via save_changes
let _ = User {
    id: 0,
    name: "Jim".to_string(),
}
.save_changes::<User>(&mut *conn)
.unwrap();

// Delete
let _ = diesel::delete(dsl::users)
    .filter(dsl::id.eq(0))
    .execute(&mut *conn)
    .unwrap();

// Transaction with multiple operations
conn.transaction::<_, diesel::result::Error, _>(|conn| {
    diesel::insert_into(dsl::users)
        .values((dsl::id.eq(0), dsl::name.eq("Jim")))
        .execute(&mut *conn)
        .unwrap();
    diesel::insert_into(dsl::users)
        .values((dsl::id.eq(1), dsl::name.eq("Another Jim")))
        .execute(&mut *conn)
        .unwrap();
    Ok(())
})
.unwrap();

} `

My dependencies: bb8 = { version = "0.8.0"} bb8-diesel = { version = "0.2.1"} diesel = { version = "2.0.2", features = ["postgres"]} tokio = { version = "1.21.2", features = ["full"] }

The error message is: error[E0277]: the trait boundDieselConnectionManager: ManageConnection` is not satisfied --> src/main.rs:23:16 23 let pool = bb8::Pool::builder().build(manager).await.unwrap(); ^^^^^^^^^^^^^^^^^^ the trait ManageConnection is not implemented for DieselConnectionManager<PgConnection>

note: required by a bound in Pool::<M>::builder --> /Users/xxxx/.cargo/registry/src/github.com-1ecc6299db9ec823/bb8-0.8.0/src/api.rs:42:9 | 42 | impl Pool { | ^^^^^^^^^^^^^^^^ required by this bound in Pool::<M>::builder

error[E0277]: the trait bound DieselConnectionManager<PgConnection>: ManageConnection is not satisfied --> src/main.rs:23:16 23 let pool = bb8::Pool::builder().build(manager).await.unwrap(); ^^^^^^^^^ the trait ManageConnection is not implemented for DieselConnectionManager<PgConnection>

note: required by a bound in Pool --> /Users/xxxx/.cargo/registry/src/github.com-1ecc6299db9ec823/bb8-0.8.0/src/api.rs:17:8 | 17 | M: ManageConnection, | ^^^^^^^^^^^^^^^^ required by this bound in Pool

error[E0277]: the trait bound DieselConnectionManager<PgConnection>: ManageConnection is not satisfied --> src/main.rs:23:37 23 let pool = bb8::Pool::builder().build(manager).await.unwrap(); ^^^^^ the trait ManageConnection is not implemented for DieselConnectionManager<PgConnection>

note: required by a bound in bb8::Builder::<M>::build --> /Users/xxxx/.cargo/registry/src/github.com-1ecc6299db9ec823/bb8-0.8.0/src/api.rs:118:9 | 118 | impl Builder { | ^^^^^^^^^^^^^^^^ required by this bound in bb8::Builder::<M>::build

error[E0277]: the trait bound DieselConnectionManager<PgConnection>: ManageConnection is not satisfied --> src/main.rs:23:51 | 23 | let pool = bb8::Pool::builder().build(manager).await.unwrap(); | ^^^^^^ the trait ManageConnection is not implemented for DieselConnectionManager<PgConnection>

error[E0277]: the trait bound DieselConnectionManager<PgConnection>: ManageConnection is not satisfied --> src/main.rs:23:16 23 let pool = bb8::Pool::builder().build(manager).await.unwrap(); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait ManageConnection is not implemented for DieselConnectionManager<PgConnection>

note: required by a bound in Pool --> /Users/xxxx/.cargo/registry/src/github.com-1ecc6299db9ec823/bb8-0.8.0/src/api.rs:17:8`

Do you have any suggestions or plans on fixing this?

Vesafary commented 1 year ago

@BeaconBrigade @smklein I also get the trait bound error. Is there an easy fix that was forgotten in the example?

BeaconBrigade commented 1 year ago

Hi @Vesafary, I've found the source of the issue. You're using an outdated version of bb8-diesel. When I made a pr to upgrade bb8-diesel to the newer version of bb8, @smklein merged my pr but didn't publish to crates.io (I'm assuming this is because they don't have permissions on the crate). This means to use the newer version you'll have to use the git version like so:

[dependencies]
bb8 = "0.8.0"
bb8-diesel = { git = "https://github.com/overdrivenpotato/bb8-diesel.git", version = "0.2.1" }
diesel = { version = "2.0.2", features = ["postgres"] }
tokio = { version = "1.22.0", features = ["full"] }

This is a bit annoying, but I don't know if this can be solved unless the original maintainer comes back. #10 shouldn't be necessary but let me know if any other issues arise.

Vesafary commented 1 year ago

Hmm, you seem to be right. Thanks for the answer, I'll remove my PR.

skylark-17 commented 1 year ago

Hi! I tried to use git version and got compiler error:

error[E0432]: unresolved import `diesel::connection::ConnectionGatWorkaround`
  --> /Users/xxx/.cargo/git/checkouts/bb8-diesel-3f90b18c23a445bc/89b7620/src/lib.rs:11:33
   |
11 |         AnsiTransactionManager, ConnectionGatWorkaround, LoadConnection, LoadRowIter,
   |                                 ^^^^^^^^^^^^^^^^^^^^^^^ no `ConnectionGatWorkaround` in `connection`

So, I can't use both crates.io and git version. @BeaconBrigade @smklein, can you watch the issue and fix at least git version, please?

My dependencies are:

[dependencies]
dotenvy = "0.15"
bb8 = "0.8.0"
bb8-diesel = { git = "https://github.com/overdrivenpotato/bb8-diesel.git", version = "0.2.1" }
diesel = { version = "2.0.2", features = ["postgres"] }
tokio = { version = "1.22.0", features = ["full"] }
BeaconBrigade commented 1 year ago

Not sure what's going on, haven't messed with this in a long time, but did you try using the tokio 1.21? That's the version the library is using so that could be it.

Looks like you are using the right version of bb8 and diesel, so other than the tokio version, I'm not sure could be wrong. Maybe check you have the right features for each library.

This could also be an issue in the library but I don't have to time to really look into it.

Vesafary commented 1 year ago

These days I am using diesel-async, which already comes with its own integration with bb8 (and some other async pools), so not using this anymore. If interested, I can share a bit more, but it should be fairly straightforward with the docs themselves.

skylark-17 commented 1 year ago

Problem is not int Tokio crate. bb8-diesel tries to import diesel::connection::ConnectionGatWorkaround which my compiler can't resolve.

I will try to use diesel-async, thanks everyone!