如何获取与柴油的关联

时间:2019-08-04 14:15:04

标签: rust rust-diesel

我正在学习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>>>>`

0 个答案:

没有答案