英文:
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<Manager<SqliteConnection>>`
Code
// 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();
}
// 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("/", get(hello))
.route("/user", Router::new())
.with_state(pool);
let addr = format!("0.0.0.0:{}", config.port);
axum::Server::bind(&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
英文:
diesel
is 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("Hello world!".into_sql::<Text>());
query.get_result::<String>(conn)
}).await?;
So my code should look like that:
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();
}
Original answer by deadpool
owner: https://github.com/bikeshedder/deadpool/issues/267#issuecomment-1647989137
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论