【发布时间】:2019-09-10 23:27:14
【问题描述】:
我正在尝试创建一个可以在柴油中用于插入的结构。具体来说,我正在使结构可插入。编译时出现此错误。
我有一个结构,我试图通过派生属性创建Insertable。我有一个名为Bounty 的字段应该代表金钱,所以我使用BigDecimal 作为类型。编译后,我得到标题中的错误。我也尝试过使用f64,但这给出了同样的错误。
#[macro_use]
extern crate diesel;
extern crate bigdecimal;
mod schema {
use bigdecimal::BigDecimal;
table! {
Threads (Id) {
Id -> Int8,
Views -> Int4,
Points -> Int4,
FlagPoints -> Int4,
IsDisabled -> Bool,
IsAnswered -> Bool,
Bounty -> Numeric,
Title -> Varchar,
Body -> Text,
UserId -> Int8,
CreatedBy -> Varchar,
CreatedOn -> Timestamptz,
LastModifiedBy -> Varchar,
LastModifiedOn -> Timestamptz,
}
}
#[allow(non_snake_case)]
#[derive(Debug, Insertable)]
#[table_name = "Threads"]
pub struct InsertableThread {
pub Bounty: BigDecimal,
pub Title: String,
pub Body: String,
pub UserId: i64
}
}
fn main() {}
我的结构在它自己的文件中,这是整个代码。 struct Thread 编译没有问题。错误发生在InsertableThread 上,因为它是使用BigDecimal 的错误。这是导致的错误。
error[E0277]: the trait bound `bigdecimal::BigDecimal: diesel::Expression` is not satisfied
--> src/main.rs:29:21
|
29 | #[derive(Debug, Insertable)]
| ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `bigdecimal::BigDecimal`
|
= note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Numeric>` for `bigdecimal::BigDecimal`
error[E0277]: the trait bound `bigdecimal::BigDecimal: diesel::Expression` is not satisfied
--> src/main.rs:29:21
|
29 | #[derive(Debug, Insertable)]
| ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `bigdecimal::BigDecimal`
|
= note: required because of the requirements on the impl of `diesel::Expression` for `&bigdecimal::BigDecimal`
= note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Numeric>` for `&bigdecimal::BigDecimal`
error[E0277]: the trait bound `bigdecimal::BigDecimal: diesel::Expression` is not satisfied
--> src/main.rs:29:21
|
29 | #[derive(Debug, Insertable)]
| ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `bigdecimal::BigDecimal`
|
= note: required because of the requirements on the impl of `diesel::Expression` for `&'insert bigdecimal::BigDecimal`
= note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Numeric>` for `&'insert bigdecimal::BigDecimal`
我正在使用 Rust 1.34、柴油 1.4.2 和 Postgres 11。
我愿意更改数据库、Postgres 或 Rust 代码中的类型。数据库使用numeric,在Rust 代码中我尝试了f64 和BigDecimal。我也愿意自己直接实现这个特征,但我需要一些指导,因为我找不到样本。
【问题讨论】:
-
提供结构
Thread的目的是什么?重现错误是否必需?请确保您的minimal reproducible example最小。 -
请尝试以可读的方式格式化您的问题。编辑器提供 UI 按钮来帮助您正确格式化,有 a help document 如果您不熟悉 Markdown,可以查看对您的问题所做的现有编辑,还有实时预览让您知道自己是什么准备提交。
-
你提供的代码没有报你问的错误。取而代之的是:属性
table_name目前对编译器来说是未知的,并且将来可能会增加它的含义。请查看如何创建minimal reproducible example,然后查看edit 您的问题以包含它。尝试在全新的 Cargo 项目中重现您的错误。有Rust-specific MCVE tips 和Diesel tips 可以用来减少您在此处发布的原始代码。 -
是否需要包含所有这些依赖项,例如“sparkpost”,才能产生错误?
Title、Bounty等字段是否必填?请确保您的minimal reproducible example最小。
标签: postgresql rust rust-diesel