4

我正在学习 Rust,我正在尝试使用 Diesel 从数据库中加载关联。我定义了以下模型:

#[derive(Identifiable, Queryable, PartialEq, Debug)]
#[table_name = "contacts"]
pub struct Contact {
    pub id: i32,
    pub firstname: String,
    pub lastname: String,
}

#[derive(Identifiable, Queryable, Associations, PartialEq, Debug)]
#[belongs_to(Contact)]
#[table_name = "emails"]
pub struct Email {
    pub id: i32, 
    pub contact_id: i32,
    pub label: String,
    pub email: String,
}

现在我正在尝试使用以下代码获取所有联系人及其相关电子邮件:

let result = match contacts::table
    .inner_join(emails::table)
    .load::<models::Contact, Vec<models::Email>>(&self.conn)
{
    Ok(rows) => rows,
    Err(_) => return Err(io::Error::new(io::ErrorKind::Other, "Database error")),
};

但这不起作用。我错过了什么?该错误与不满足的特征界限有关。

完整的错误信息:

error[E0107]: wrong number of type arguments: expected 1, found 2
  --> src/db.rs:44:38
   |
44 |             .load::<models::Contact, Vec<models::Email>>(&self.conn) {
   |                                      ^^^^^^^^^^^^^^^^^^ unexpected type argument

error[E0277]: the trait bound `(i32, std::string::String, std::string::String): diesel::Queryable<((diesel::sql_types::Integer, diesel::sql_types::Text, diesel::sql_types::Text), (diesel::sql_types::Integer, diesel::sql_types::Nullable<diesel::sql_types::Integer>, diesel::sql_types::Text, diesel::sql_types::Text)), _>` is not satisfied
  --> src/db.rs:44:14
   |
44 |             .load::<models::Contact, Vec<models::Email>>(&self.conn) {
   |              ^^^^ the trait `diesel::Queryable<((diesel::sql_types::Integer, diesel::sql_types::Text, diesel::sql_types::Text), (diesel::sql_types::Integer, diesel::sql_types::Nullable<diesel::sql_types::Integer>, diesel::sql_types::Text, diesel::sql_types::Text)), _>` is not implemented for `(i32, std::string::String, std::string::String)`
   |
   = help: the following implementations were found:
             <(A, B, C) as diesel::Queryable<(SA, SB, SC), __DB>>
             <(A, B, C) as diesel::Queryable<diesel::sql_types::Record<(SA, SB, SC)>, diesel::pg::Pg>>
   = note: required because of the requirements on the impl of `diesel::Queryable<((diesel::sql_types::Integer, diesel::sql_types::Text, diesel::sql_types::Text), (diesel::sql_types::Integer, diesel::sql_types::Nullable<diesel::sql_types::Integer>, diesel::sql_types::Text, diesel::sql_types::Text)), _>` for `models::Contact`
   = note: required because of the requirements on the impl of `diesel::query_dsl::LoadQuery<_, models::Contact>` for `diesel::query_builder::SelectStatement<diesel::query_source::joins::JoinOn<diesel::query_source::joins::Join<schema::contacts::table, schema::emails::table, diesel::query_source::joins::Inner>, diesel::expression::operators::Eq<diesel::expression::nullable::Nullable<schema::emails::columns::contact_id>, diesel::expression::nullable::Nullable<schema::contacts::columns::id>>>>`
4

0 回答 0