You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Axum集成deadpool_diesel报错:LoadConnection trait未实现于指定类型

问题解决:deadpool_diesel 与 Diesel 集成的 LoadConnection 未实现错误

错误原因

你遇到的错误是因为 deadpool::managed::Object<Manager<SqliteConnection>> 是 Deadpool 对数据库连接的包装智能指针,而 Diesel 的 load 方法需要直接操作底层的 SqliteConnection 实例,而非这个包装类型。

解决方案

有两种简单的修复方式:

方式1:直接解引用获取底层连接

利用 Deadpool Object 实现的 DerefMut 特性,直接解引用获取内部连接的可变引用:

// 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) // 解引用获取实际连接
        .unwrap();
}

方式2:使用 Deadpool 提供的 interact 方法

该方法会帮你安全地处理连接的访问逻辑,避免手动解引用:

// user.rs 修改后代码
pub async fn get_all_users(State(pool): State<Pool>) {
    let all_users = pool.interact(|conn| {
        users
            .select(UserInformation::as_select())
            .load::<UserInformation>(conn)
    }).await.unwrap().unwrap();
}

额外注意事项

确保你的 Cargo.toml 中依赖版本兼容,示例配置如下:

[dependencies]
axum = "0.7"
diesel = { version = "2.1", features = ["sqlite", "r2d2"] }
deadpool-diesel = { version = "0.12", features = ["sqlite", "tokio"] }
tokio = { version = "1.0", features = ["full"] }

内容的提问来源于stack exchange,提问作者MidKnight

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 05:10:05