将Option <t>与Diesel的可插入特征一起使用

时间:2019-04-25 23:57:26

标签: rust rust-diesel

我有以下模型:

use diesel::prelude::*;

use crate::schema::category;

#[derive(Debug, Identifiable, Queryable)]
#[table_name = "category"]
pub struct Category {
    pub id: i64,
    pub name: String,
    pub description: String,
    pub parent_id: Option<i64>,
}

#[derive(Debug, Insertable)]
#[table_name = "category"]
pub struct NewCategory<'a> {
    pub name: &'a str,
    pub description: &'a str,
    pub parent_id: &'a Option<i64>,
}

和schema.rs:

table! {
    category (id) {
        id -> Integer,
        name -> Text,
        description -> Text,
        parent_id -> Nullable<Integer>,
    }
}

但是,当我尝试编译此代码时,出现以下错误:

error[E0277]: the trait bound `std::option::Option<i64>: diesel::Expression` is not satisfied
  --> src/models/categories.rs:15:17
   |
15 | #[derive(Debug, Insertable)]
   |                 ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `std::option::Option<i64>`
   |
   = note: required because of the requirements on the impl of `diesel::Expression` for `&std::option::Option<i64>`

error[E0277]: the trait bound `std::option::Option<i64>: diesel::Expression` is not satisfied
  --> src/models/categories.rs:15:17
   |
15 | #[derive(Debug, Insertable)]
   |                 ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `std::option::Option<i64>`
   |
   = note: required because of the requirements on the impl of `diesel::Expression` for `&'a std::option::Option<i64>`

我需要什么才能使其正常工作?我环顾四周,但发现的唯一类似问题是某人的表中有16列以上,而这里不是这种情况。

1 个答案:

答案 0 :(得分:0)

修改pub parent_id: &'a Option<i64>,将&'a放置在选项pub parent_id: Option<&'a i64>内。