I'm currently creating an API in Rust using Axum
and deadpool_diesel
for the database queries.
I'm encountering an error that I don't really understand and would need some help to fix it.
I'm currently discovering Diesel
so I'm not a master at all of that crate.
error[E0277]: the trait bound `(diesel::sql_types::Binary, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Bool, diesel::sql_types::Bool, diesel::sql_types::Nullable<diesel::sql_types::Binary>, diesel::sql_types::Timestamp, diesel::sql_types::Nullable<diesel::sql_types::Timestamp>, diesel::sql_types::Timestamp): load_dsl::private::CompatibleType<UserInformation, _>` is not satisfied
--> src/controller/user.rs:11:51
|
11 | let all_users = users.load::<UserInformation>(&mut &conn).unwrap();
| ---- ^^^^^^^^^^ the trait `load_dsl::private::CompatibleType<UserInformation, _>` is not implemented for `(diesel::sql_types::Binary, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Bool, diesel::sql_types::Bool, diesel::sql_types::Nullable<diesel::sql_types::Binary>, diesel::sql_types::Timestamp, diesel::sql_types::Nullable<diesel::sql_types::Timestamp>, diesel::sql_types::Timestamp)`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `load_dsl::private::CompatibleType<U, DB>`:
(ST0, ST1)
(ST0, ST1, ST2)
(ST0, ST1, ST2, ST3)
(ST0, ST1, ST2, ST3, ST4)
(ST0, ST1, ST2, ST3, ST4, ST5)
(ST0, ST1, ST2, ST3, ST4, ST5, ST6)
(ST0, ST1, ST2, ST3, ST4, ST5, ST6, ST7)
(ST0, ST1, ST2, ST3, ST4, ST5, ST6, ST7, ST8)
and 24 others
= note: required for `table` to implement `LoadQuery<'_, _, UserInformation>`
note: required by a bound in `diesel::RunQueryDsl::load`
--> /Users/midknight/.cargo/registry/src/index.crates.io-6f17d22bba15001f/diesel-2.1.0/src/query_dsl/mod.rs:1543:15
|
1541 | fn load<'query, U>(self, conn: &mut Conn) -> QueryResult<Vec<U>>
| ---- required by a bound in this associated function
1542 | where
1543 | Self: LoadQuery<'query, Conn, U>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `RunQueryDsl::load`
error[E0277]: the trait bound `UserInformation: FromSqlRow<_, _>` is not satisfied
--> src/controller/user.rs:11:51
|
11 | let all_users = users.load::<UserInformation>(&mut &conn).unwrap();
| ---- ^^^^^^^^^^ the trait `FromSqlRow<_, _>` is not implemented for `UserInformation`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `FromSqlRow<ST, DB>`:
<(T1, T0) as FromSqlRow<(ST1, Untyped), __DB>>
<(T1, T2, T0) as FromSqlRow<(ST1, ST2, Untyped), __DB>>
<(T1, T2, T3, T0) as FromSqlRow<(ST1, ST2, ST3, Untyped), __DB>>
<(T1, T2, T3, T4, T0) as FromSqlRow<(ST1, ST2, ST3, ST4, Untyped), __DB>>
<(T1, T2, T3, T4, T5, T0) as FromSqlRow<(ST1, ST2, ST3, ST4, ST5, Untyped), __DB>>
<(T1, T2, T3, T4, T5, T6, T0) as FromSqlRow<(ST1, ST2, ST3, ST4, ST5, ST6, Untyped), __DB>>
<(T1, T2, T3, T4, T5, T6, T7, T0) as FromSqlRow<(ST1, ST2, ST3, ST4, ST5, ST6, ST7, Untyped), __DB>>
<(T1, T2, T3, T4, T5, T6, T7, T8, T0) as FromSqlRow<(ST1, ST2, ST3, ST4, ST5, ST6, ST7, ST8, Untyped), __DB>>
and 23 others
= note: required for `table` to implement `LoadQuery<'_, _, UserInformation>`
note: required by a bound in `diesel::RunQueryDsl::load`
--> /Users/midknight/.cargo/registry/src/index.crates.io-6f17d22bba15001f/diesel-2.1.0/src/query_dsl/mod.rs:1543:15
|
1541 | fn load<'query, U>(self, conn: &mut Conn) -> QueryResult<Vec<U>>
| ---- required by a bound in this associated function
1542 | where
1543 | Self: LoadQuery<'query, Conn, U>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `RunQueryDsl::load`
error[E0277]: the trait bound `&deadpool::managed::Object<Manager<SqliteConnection>>: LoadConnection` is not satisfied
--> src/controller/user.rs:11:51
|
11 | let all_users = users.load::<UserInformation>(&mut &conn).unwrap();
| ---- ^^^^^^^^^^ the trait `LoadConnection` is not implemented for `&deadpool::managed::Object<Manager<SqliteConnection>>`
| |
| required by a bound introduced by this call
|
= help: the trait `LoadConnection` is implemented for `SqliteConnection`
= note: required for `table` to implement `LoadQuery<'_, &deadpool::managed::Object<Manager<SqliteConnection>>, UserInformation>`
note: required by a bound in `diesel::RunQueryDsl::load`
--> /Users/midknight/.cargo/registry/src/index.crates.io-6f17d22bba15001f/diesel-2.1.0/src/query_dsl/mod.rs:1543:15
|
1541 | fn load<'query, U>(self, conn: &mut Conn) -> QueryResult<Vec<U>>
| ---- required by a bound in this associated function
1542 | where
1543 | Self: LoadQuery<'query, Conn, U>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `RunQueryDsl::load`
use axum::extract::State;
use deadpool_diesel::sqlite::Pool;
use diesel::RunQueryDsl;
use crate::{schema::users::dsl::*, models::user::UserInformation};
pub async fn get_all_users(State(pool): State<Pool>) {
let conn = pool.get().await.unwrap();
let all_users = users.load::<UserInformation>(&mut &conn).unwrap();
}
// schema.rs
diesel::table! {
users {
id -> Binary,
username -> Text,
name -> Text,
surname -> Text,
email -> Text,
password_hash -> Text,
disabled -> Bool,
superuser -> Bool,
profile_picture -> Nullable<Binary>,
creation_date -> Timestamp,
last_login -> Nullable<Timestamp>,
last_update -> Timestamp,
}
}
// model.rs
use chrono::NaiveDateTime;
use diesel::{Insertable, Selectable, Queryable};
use crate::schema::users;
#[derive(Selectable)]
#[diesel(table_name = users)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct UserInformation {
#[diesel(sql_type = Binary)]
pub id: Vec<u8>,
#[diesel(sql_type = Text)]
pub username: String,
#[diesel(sql_type = Text)]
pub name: String,
#[diesel(sql_type = Text)]
pub surname: String,
#[diesel(sql_type = Text)]
pub email: String,
#[diesel(sql_type = Bool)]
pub disabled: bool,
#[diesel(sql_type = Bool)]
pub superuser: bool,
#[diesel(sql_type = Nullable<Binary>)]
pub profile_picture: Option<Vec<u8>>,
#[diesel(sql_type = Timestamp)]
pub creation_date: NaiveDateTime,
#[diesel(sql_type = Nullable<Timestamp>)]
pub last_login: Option<NaiveDateTime>,
#[diesel(sql_type = Timestamp)]
pub last_update: NaiveDateTime,
}
Your struct is missing the required FromSqlRow
implementation which can be implemented by deriving Queryable
:
#[derive(Selectable, Queryable)] // <----------
#[diesel(table_name = users)]
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
pub struct UserInformation {
// ...
}
You are also missing the password_hash
field that your table has. Either add it to the model definition, or if you have omitted it on purpose you can add a SELECT
clause to only get those fields (since you've already derived Selectable
):
let all_users = users
.select(UserInformation::as_select()) // <----------
.load::<UserInformation>(&mut conn)
.unwrap();