我正在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 了f64BigDecimal.我也愿意自己直接实现这个特性,但我需要一些关于如何实现的指导,因为我找不到样本.

推荐答案

Diesel使用Cargo features Select 加入增强功能.

我还没有找到一个清晰的文档页面,但它们列在its Cargo.toml中:

[features]
default = ["with-deprecated", "32-column-tables"]
extras = ["chrono", "serde_json", "uuid", "deprecated-time", "network-address", "numeric", "r2d2"]
unstable = ["diesel_derives/nightly"]
large-tables = ["32-column-tables"]
huge-tables = ["64-column-tables"]
x32-column-tables = ["32-column-tables"]
32-column-tables = []
x64-column-tables = ["64-column-tables"]
64-column-tables = ["32-column-tables"]
x128-column-tables = ["128-column-tables"]
128-column-tables = ["64-column-tables"]
postgres = ["pq-sys", "bitflags", "diesel_derives/postgres"]
sqlite = ["libsqlite3-sys", "diesel_derives/sqlite"]
mysql = ["mysqlclient-sys", "url", "diesel_derives/mysql"]
with-deprecated = []
deprecated-time = ["time"]
network-address = ["ipnetwork", "libc"]
numeric = ["num-bigint", "bigdecimal", "num-traits", "num-integer"]

您需要启用numeric功能,并确保使用与Diesel兼容的bigdecimal版本:

[dependencies]
diesel = { version = "1.4.2", features = ["numeric"] }
bigdecimal = "0.0.14"

代码编译:

#[macro_use]
extern crate diesel;

use crate::schema::threads;
use bigdecimal::BigDecimal;

mod schema {
    table! {
        threads (id) {
            id -> Int4,
            bounty -> Numeric,
        }
    }
}

#[derive(Debug, Insertable)]
#[table_name = "threads"]
pub struct InsertableThread {
    pub bounty: BigDecimal,
}

另见:

Rust相关问答推荐

从Rust调用C++虚拟方法即使在成功执行之后也会引发Access违规错误

使用模块中的所有模块,但不包括特定模块

通过使用光标拖动角来绕其中心旋转矩形

编译项目期间使用Cargo生成时出现rustc错误

当T不执行Copy时,如何返回Arc Mutex T后面的值?

我应该将哪些文件放入我的GitHub存储库

如何修复&q;无法返回引用函数参数的值在异步规则中返回引用当前函数&q;拥有的数据的值?

为什么 `Deref` 没有在 `Cell` 上实现?

`use` 和 `crate` 关键字在 Rust 项目中效果不佳

将引用移动到线程中

枚举的利基优化如何在 Rust 中工作?

从 Rust 中的 if/else 中的引用创建 MappedRwLockWriteGuard

为什么数组不像向量那样在 for 块之后移动?

Rust 引用元组和引用元组

将 `&T` 转换为新类型 `&N`

If let expression within .iter().any

字符串切片的向量超出范围但原始字符串仍然存在,为什么判断器说有错误?

以下打印数组每个元素的 Rust 代码有什么问题?

如何在没有 `make_contiguous()` 的情况下对 VecDeque 进行排序或反转?

为什么 u64::trailing_zeros() 在无分支工作时生成分支程序集?