mehcode / tokio-diesel

Integrate Diesel into Tokio cleanly and efficiently.
Apache License 2.0
104 stars 30 forks source link

Build fails on Diesel v2/master (doesn't satisfy RunQueryDsl) #22

Open oeed opened 4 years ago

oeed commented 4 years ago

I'm aware Diesel version 2 isn't stable yet, so understand if this wouldn't be a priority. However, I've found I can't get the example code for tokio-diesel to compile with the Diesel v2.0/master branch. I'm using it primarily because it avoids the need for extern crate, I'm still pretty new to Rust so I had some headaches when forced to use extern crate.

I've made a reproduction using the given example.rs file, although note I've replaced UUID with plain integers as it was causing other issues.

The full output of the first error is (the second is basically the same but for SelectStatement)

error[E0599]: no method named `execute_async` found for struct `diesel::query_builder::InsertStatement<users::table, diesel::query_builder::ValuesClause<diesel::insertable::ColumnInsertValue<users::columns::id, diesel::expression::bound::Bound<diesel::sql_types::Integer, i32>>, users::table>>` in the current scope
   --> src/main.rs:29:10
    |
29  |         .execute_async(&pool)
    |          ^^^^^^^^^^^^^ method not found in `diesel::query_builder::InsertStatement<users::table, diesel::query_builder::ValuesClause<diesel::insertable::ColumnInsertValue<users::columns::id, diesel::expression::bound::Bound<diesel::sql_types::Integer, i32>>, users::table>>`
    | 
   ::: /Users/olivercooper/.cargo/git/checkouts/diesel-6e3331fb3b9331ec/56232d9/diesel/src/query_builder/insert_statement/mod.rs:129:1
    |
129 | pub struct InsertStatement<T, U, Op = Insert, Ret = NoReturningClause> {
    | ----------------------------------------------------------------------
    | |
    | doesn't satisfy `_: diesel::query_dsl::RunQueryDsl<_>`
    | doesn't satisfy `_: tokio_diesel::AsyncRunQueryDsl<_, diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<_>>>`
    |
    = note: the method `execute_async` exists but the following trait bounds were not satisfied:
            `diesel::query_builder::InsertStatement<users::table, diesel::query_builder::ValuesClause<diesel::insertable::ColumnInsertValue<users::columns::id, diesel::expression::bound::Bound<diesel::sql_types::Integer, i32>>, users::table>>: diesel::query_dsl::RunQueryDsl<_>`
            which is required by `diesel::query_builder::InsertStatement<users::table, diesel::query_builder::ValuesClause<diesel::insertable::ColumnInsertValue<users::columns::id, diesel::expression::bound::Bound<diesel::sql_types::Integer, i32>>, users::table>>: tokio_diesel::AsyncRunQueryDsl<_, diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<_>>>`
            `&diesel::query_builder::InsertStatement<users::table, diesel::query_builder::ValuesClause<diesel::insertable::ColumnInsertValue<users::columns::id, diesel::expression::bound::Bound<diesel::sql_types::Integer, i32>>, users::table>>: diesel::query_dsl::RunQueryDsl<_>`
            which is required by `&diesel::query_builder::InsertStatement<users::table, diesel::query_builder::ValuesClause<diesel::insertable::ColumnInsertValue<users::columns::id, diesel::expression::bound::Bound<diesel::sql_types::Integer, i32>>, users::table>>: tokio_diesel::AsyncRunQueryDsl<_, diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<_>>>`
            `&mut diesel::query_builder::InsertStatement<users::table, diesel::query_builder::ValuesClause<diesel::insertable::ColumnInsertValue<users::columns::id, diesel::expression::bound::Bound<diesel::sql_types::Integer, i32>>, users::table>>: diesel::query_dsl::RunQueryDsl<_>`
            which is required by `&mut diesel::query_builder::InsertStatement<users::table, diesel::query_builder::ValuesClause<diesel::insertable::ColumnInsertValue<users::columns::id, diesel::expression::bound::Bound<diesel::sql_types::Integer, i32>>, users::table>>: tokio_diesel::AsyncRunQueryDsl<_, diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<_>>>`
pksunkara commented 3 years ago

If you clone the repo locally (and also diesel), apply this patch, and it should work.

commit 0fc88e4318604c1293f4e697b00e078412b66277
Author: Pavan Kumar Sunkara <pavan.sss1991@gmail.com>
Date:   Sat Nov 14 10:37:02 2020 +0100

    Upgrade tokio to diesel v2

diff --git a/Cargo.toml b/Cargo.toml
index 2a6a50f..636d43d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -10,12 +10,12 @@ categories = ["asynchronous", "database"]

 [dependencies]
 async-trait = "0.1.33"
-diesel = { version = "1.4.5", default-features = false, features = [ "r2d2" ] }
+diesel = { path = "../diesel/diesel", default-features = false, features = [ "r2d2" ] }
 futures = { version = "0.3.5", default-features = false }
 r2d2 = "0.8.8"
 tokio = { version = ">=0.2.21", default-features = false, features = [ "blocking", "rt-threaded" ] }

 [dev-dependencies]
-diesel = { version = "1.4.4", default-features = false, features = [ "postgres", "uuidv07" ] }
+diesel = { path = "../diesel/diesel", default-features = false, features = [ "postgres", "uuidv07" ] }
 uuid = { version = "0.8.1", features = [ "v4" ] }
 tokio = { version = ">=0.2.21", default-features = false, features = [ "full" ] }
diff --git a/src/lib.rs b/src/lib.rs
index 1b27a41..49bec3e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,7 +6,7 @@ use diesel::{
         methods::{ExecuteDsl, LimitDsl, LoadQuery},
         RunQueryDsl,
     },
-    r2d2::{ConnectionManager, Pool},
+    r2d2::{ConnectionManager, Pool, R2D2Connection},
     result::QueryResult,
     Connection,
 };
@@ -67,7 +67,7 @@ where
 #[async_trait]
 impl<Conn> AsyncSimpleConnection<Conn> for Pool<ConnectionManager<Conn>>
 where
-    Conn: 'static + Connection,
+    Conn: 'static + R2D2Connection,
 {
     #[inline]
     async fn batch_execute_async(&self, query: &str) -> AsyncResult<()> {
@@ -99,7 +99,7 @@ where
 #[async_trait]
 impl<Conn> AsyncConnection<Conn> for Pool<ConnectionManager<Conn>>
 where
-    Conn: 'static + Connection,
+    Conn: 'static + R2D2Connection,
 {
     #[inline]
     async fn run<R, Func>(&self, f: Func) -> AsyncResult<R>
@@ -163,7 +163,7 @@ where
 impl<T, Conn> AsyncRunQueryDsl<Conn, Pool<ConnectionManager<Conn>>> for T
 where
     T: Send + RunQueryDsl<Conn>,
-    Conn: 'static + Connection,
+    Conn: 'static + R2D2Connection,
 {
     async fn execute_async(self, asc: &Pool<ConnectionManager<Conn>>) -> AsyncResult<usize>
     where