我正在try 创建一个可以在diesel中用于插入的 struct .具体来说,我正在使 struct 可插入.编译时,我得到了这个错误.
我有一个 struct ,我正试图通过派生属性生成Insertable
.我有一个名为Bounty
的字段,它应该代表钱,所以我使用BigDecimal
作为类型.编译后,我得到了标题中的错误.我也try 过使用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 放在它自己的文件里,这是完整的代码.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、diesel 1.4.2和Postgres 11.
我愿意更改数据库、Postgres或Rust代码中的类型.数据库使用numeric
,在 rust 代码中,我try 了f64
和BigDecimal
.我也愿意自己直接实现这个特性,但我需要一些关于如何实现的指导,因为我找不到样本.