I have this sql migration query for the account entity:
CREATE TABLE accounts (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES users(id),
name VARCHAR NOT NULL,
currency VARCHAR NOT NULL,
account_type VARCHAR NOT NULL,
amount DOUBLE PRECISION NOT NULL
);
This table generates the following schema:
diesel::table! {
use diesel::sql_types::*;
accounts (id) {
id -> Uuid,
user_id -> Uuid,
name -> Varchar,
currency -> Varchar,
account_type -> Varchar,
amount -> Float8,
}
}
And here is the struct itself:
#[derive(Debug, Serialize, Deserialize, Clone, Queryable, PartialEq)]
#[diesel(belongs_to(User, foreign_key = user_id))]
#[diesel(table_name = accounts)]
pub struct Account {
id: Uuid,
user_id: Uuid,
name: String,
currency: String,
account_type: String,
amount: f64,
created_date: DateTime<Utc>,
}
There is no error for any of the traits in derive call, but when I try to make any operation with this table, e.g. this one:
let new_account = diesel::insert_into(accounts::table)
.values(&account_data)
.get_result::<Account>(&mut db.pool.get().unwrap())
.map_err(|_| "Failed to create account".to_string());
I see the following error under the passed db.poll into get_result method:
the trait bound `(uuid::Uuid, uuid::Uuid, std::string::String, std::string::String, std::string::String, f64, chrono::DateTime<Utc>): diesel::Queryable<(diesel::sql_types::Uuid, diesel::sql_types::Uuid, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Text, Double), _>` is not satisfied
the following other types implement trait `diesel::Queryable<ST, DB>`:
<(std::collections::Bound<T>, std::collections::Bound<T>) as diesel::Queryable<diesel::sql_types::Range<ST>, Pg>>
<(A,) as diesel::Queryable<Record<(SA,)>, Pg>>
<(A,) as diesel::Queryable<(SA,), __DB>>
<(A, B) as diesel::Queryable<Record<(SA, SB)>, Pg>>
<(A, B) as diesel::Queryable<(SA, SB), __DB>>
<(A, B, C) as diesel::Queryable<Record<(SA, SB, SC)>, Pg>>
<(A, B, C) as diesel::Queryable<(SA, SB, SC), __DB>>
<(A, B, C, D) as diesel::Queryable<Record<(SA, SB, SC, SD)>, Pg>>
and 57 others
required for `InsertStatement<table, ValuesClause<(ColumnInsertValue<user_id, Bound<Uuid, &...>>, ..., ..., ..., ...), ...>>` to implement `LoadQuery<_, Account>`
Obviously I can’t remove the Queryable
trait since it’s mandatory to use with by diesel.
2
Answers
I ended up having additional
created_date
field as @kmdreko mentioned in the comment, but for some reason this field was not highlighted as the one that caused the issue. This is very unintuitive and I know that usually diesel highlights fields if they are incorrect according to the schema provided, so I have no idea why it didn't work that way here.That’s essentially the first error message in this list. It’s a mismatch between what your query returns and what your struct expects the query to return. This information can only be resolved at the place where you actually execute the query.
You can prevent such errors from happening by using
#[derive(Selectable)]
+#[diesel(check_for_backend(diesel::pg::Pg))]
on your struct and.select(YourType::as_select())
in your query.