【问题标题】:#[derive(Insertable)] is not implemented for `std::string::String`#[derive(Insertable)] 没有为 `std::string::String` 实现
【发布时间】:2018-10-23 16:28:56
【问题描述】:

我收到此错误:

#[derive(Insertable, Queryable, Identifiable, Debug, PartialEq)]
         ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `std::string::String`

当我尝试编译这个struct:

#[derive(Insertable, Queryable, Identifiable, Debug, PartialEq)]
#[table_name = "example_table"]
pub struct SigninLog {
    pub id: i32,
    pub user_group: UserRoleEnum,
    pub created_at: Option<SystemTime>,
    pub optional_data: Option<String>
}

是因为它包含自定义enumOption&lt;String&gt;?如果这是问题,我该如何解决?

【问题讨论】:

  • 请查看如何提供minimal reproducible example。对于柴油相关的问题,see its specific MCVE guidance。提供 schema 非常重要。你还没有定义UserRoleEnum 是什么,也没有定义SystemTime 来自哪里。完全有可能甚至不需要这些字段,这是使其最小化的一部分。
  • 您解决了这个问题吗? @tandry syawaludin

标签: rust rust-diesel


【解决方案1】:

一般来说,为了能够将#[derive(X)] 用于struct,它的所有成员也必须实现XInsertable 可能不是这种情况,因为它不是标准特征,但您可能想要验证这一点;在您的情况下,Insertable 未针对 optional_data 内的 String 实现;它是为Option&lt;T&gt; 实现的,所以包含它的Option 不是问题。

您可能希望手动实现Insertable;不过,我还没有使用过diesel - 可能有更简单的方法来做到这一点。

【讨论】:

  • 其实 optional_data 的类型是 postgres 中的 json。你知道什么是最好的结构类型吗?
  • @tandrysyawaludin 我建议查看Serde's JSON capabilities
  • 如果我想要非结构化 JSON 怎么办?我的意思是用户发布的任何结构
  • "为了能够 #[derive(X)] 获取 struct,其所有成员也必须实现 X。"这不是真的。见this crate of mine for an example
  • @ljedrz 好吧,Insertable 不是标准特征,所以它可以为所欲为
猜你喜欢
  • 1970-01-01
  • 2015-10-08
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-01
  • 1970-01-01
  • 2010-11-30
相关资源
最近更新 更多