the trait `LoadConnection` is not implemented for `deadpool::managed::Object<Manager<SqliteConnection>>`

huangapple go评论67阅读模式
英文:

the trait `LoadConnection` is not implemented for `deadpool::managed::Object<Manager<SqliteConnection>>`

问题

// user.rs

pub async fn get_all_users(State(pool): State<Pool>) {
    let mut conn = pool.get().await.unwrap();

    let all_users = users
    .select(UserInformation::as_select())
    .load::<UserInformation>(&mut conn) // <---------- The error is appearing on the &mut conn
    .unwrap();
}
英文:

Context

I'm currently trying to implement deadpool_diesel and diesel to my api made with Axum but I'm getting an error that I'm not sure to understand.

My guess is that I'm maybe doing something wrong with the usage of State but I'm still discovering Axum and Diesel so I don't really know for sure

Error

the trait `LoadConnection` is not implemented for `deadpool::managed::Object&lt;Manager&lt;SqliteConnection&gt;&gt;`

Code

// user.rs

pub async fn get_all_users(State(pool): State&lt;Pool&gt;) {
    let mut conn = pool.get().await.unwrap();

    let all_users = users
    .select(UserInformation::as_select())
    .load::&lt;UserInformation&gt;(&amp;mut conn) // &lt;---------- The error is appearing on the &amp;mut conn
    .unwrap();
}
// main.rs

#[tokio::main]
async fn main() {
    let pool_manager = Manager::new(Config::get_db_path(), Runtime::Tokio1);
    let pool = Pool::builder(pool_manager)
        .max_size(50)
        .build()
        .unwrap();

    let app = Router::new()
        .route(&quot;/&quot;, get(hello))
        .route(&quot;/user&quot;, Router::new())
        .with_state(pool);

    let addr = format!(&quot;0.0.0.0:{}&quot;, config.port);

    axum::Server::bind(&amp;addr.parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap();
}

答案1

得分: 1

diesel 不支持异步操作,因此 deadpool-diesel 在其周围添加了一个同步包装器。您需要在对象上调用 interact 方法,将闭包传递给它,就像示例代码中的那样:

let result = conn.interact(|conn| {
    let query = select("Hello world!".into_sql::<Text>());
    query.get_result::<String>(conn)
}).await?;

因此,您的代码应该如下所示:

pub async fn get_all_users(State(pool): State<Pool>) {
    let mut conn = pool.get().await.unwrap();
   
    let all_users = conn.interact(|conn| users
        .select(UserInformation::as_select())
        .load::<UserInformation>(&mut conn)
        .unwrap()
    ).await.unwrap();
}

deadpool 所有者的原始回答:https://github.com/bikeshedder/deadpool/issues/267#issuecomment-1647989137

英文:

dieselis not async and therefore deadpool-diesel added a sync wrapper around it. You need to call interact on the object passing it a closure like in the example code:

let result = conn.interact(|conn| {
    let query = select(&quot;Hello world!&quot;.into_sql::&lt;Text&gt;());
    query.get_result::&lt;String&gt;(conn)
}).await?;

So my code should look like that:

pub async fn get_all_users(State(pool): State&lt;Pool&gt;) {
    let mut conn = pool.get().await.unwrap();
   
    let all_users = conn.interact(|conn| users
        .select(UserInformation::as_select())
        .load::&lt;UserInformation&gt;(&amp;mut conn)
        .unwrap()
    ).await.unwrap();
}

Original answer by deadpool owner: https://github.com/bikeshedder/deadpool/issues/267#issuecomment-1647989137

huangapple
  • 本文由 发表于 2023年7月24日 18:33:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定