sfackler / r2d2

A generic connection pool for Rust
Apache License 2.0
1.51k stars 82 forks source link

the trait `diesel::Connection` is not implemented for `r2d2::PooledConnection<r2d2_diesel::ConnectionManager<diesel::pg::PgConnection>>` #37

Closed rbalicki2 closed 7 years ago

rbalicki2 commented 7 years ago

Hello,

I have a connection pool, implemented like in writing a github webhook in rust pt 1, code at this link. I'm not sure what I am doing wrong, but when I call

  let conn: db::DbConnection = db_pool.get().chain_err(|| "Could not connect to DB")?;

  let returned_user: User = diesel::insert(&user_data)
    .into(schema::users::table)
    .get_result(&conn)
    .chain_err(|| "Error inserting user")?;

I get the following error message:

error[E0277]: the trait bound `r2d2::PooledConnection<r2d2_diesel::ConnectionManager<diesel::pg::PgConnection>>: diesel::Connection` is not satisfied
  --> src/app/mod.rs:45:6
   |
45 |     .get_result(&conn)
   |      ^^^^^^^^^^ the trait `diesel::Connection` is not implemented for `r2d2::PooledConnection<r2d2_diesel::ConnectionManager<diesel::pg::PgConnection>>`

diesel::pg::PgConnection implements diesel::Connection. Is there any way to convert from a pooled connection to a regular connection? It doesn't look like it from the docs.

My workaround is to call the establish_connection method (from the same link as above), which returns a PgConnection. However, grabbing a pooled connection or a regular connection depending on whether we're inserting or querying seems really fishy.

Am I doing something wrong? Thank you!

sfackler commented 7 years ago

PooledConnection derefs to PgConnection, so .get_result(&*conn) should work.

rbalicki2 commented 7 years ago

Yes, this works - thank you! I forgot the deref. Time to brush up...

frol commented 6 years ago

Well, this &*conn deserves to be documented... I am new to diesel and r2d2, so if somebody more knowledgeable adds a nice example to the README or documentation, that would be great!

Thank you!