特征`diesel :: Expression`没有为`bigdecimal :: BigDecimal`实现

问题描述 投票:-1回答:1

我正在尝试创建一个可以用于柴油插入的结构。具体来说,我正在使结构插入。在编译时我得到这个错误。

我有一个结构,我试图通过derive属性制作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代码中我尝试了f64BigDecimal。我也愿意直接自己实施这个特性,但是我需要一些关于如何做到这一点的指导,因为我找不到样品。

postgresql rust rust-diesel
1个回答
0
投票

Diesel使用Cargo features选择增强功能。

我没有找到这些明确的文档页面,但它们列在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"]

您需要启用数字功能并确保使用与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,
}

也可以看看:

© www.soinside.com 2019 - 2024. All rights reserved.