Использование опции <T>с вставляемой чертой Diesel - PullRequest
0 голосов
/ 26 апреля 2019

У меня есть следующая модель:

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 голосов
/ 26 апреля 2019

Измените pub parent_id: &'a Option<i64>, чтобы разместить &'a внутри опции: pub parent_id: Option<&'a i64>.

...