使用选项 与Diesel的可插入特征

问题描述 投票:0回答:1

我有以下型号:

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列时,这不是这里的情况。

rust rust-diesel
1个回答
0
投票

修改pub parent_id: &'a Option<i64>以将&'a放置在Option:pub parent_id: Option<&'a i64>中。

© www.soinside.com 2019 - 2024. All rights reserved.