gpt4 book ai didi

rust - 将 Option 与 Diesel 的 Insertable 特性结合使用

转载 作者:行者123 更新时间:2023-11-29 08:31:52 29 4
gpt4 key购买 nike

我有以下模型:

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

最佳答案

修改pub parent_id: &'a Option<i64>放置 &'a选项内部:pub parent_id: Option<&'a i64> .

关于rust - 将 Option<T> 与 Diesel 的 Insertable 特性结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55859061/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com