没有为`bigdecimal :: BigDecimal`实现特性`diesel :: Expression`。 [英] the trait `diesel::Expression` is not implemented for `bigdecimal::BigDecimal`

查看:215
本文介绍了没有为`bigdecimal :: BigDecimal`实现特性`diesel :: Expression`。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个可以在柴油中用于插入的结构。具体来说,我将结构插入。编译时出现此错误。

I am trying to create a struct that I can use in diesel for insertion. Specifically I am making the struct Insertable. On compile I get this error.

我有一个结构,我试图通过derive属性使可插入。我有一个名为 Bounty 的字段,该字段应该代表金钱,所以我使用 BigDecimal 作为类型。编译后,出现标题错误。我也尝试过使用 f64 ,但这会给出相同的错误。

I have a struct that I am trying to make Insertable via the derive attribute. I have a field called Bounty which is supposed to represent money, so I'm using BigDecimal as the type. Upon compilation, I get the error in the title. I've also tried using f64 but that gives the same error.

#[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() {}

我有我的结构在它自己的文件中,这是完整的代码。结构 Thread 编译没有问题。该错误发生在 InsertableThread 上,因为它是使用 BigDecimal 的错误。这是导致的错误。

I have my struct inside it's own file and this is the entire code. The struct Thread compiles without issue. The error happens on InsertableThread as it is the one using BigDecimal. This is the error that results.

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。

I am using Rust 1.34, diesel 1.4.2 and Postgres 11.

我愿意更改数据库类型,即Postgres,或在Rust代码中。数据库使用数字,在Rust代码中,我尝试了 f64 BigDecimal 。我也愿意直接自己实现该特性,但是由于找不到示例,因此需要一些指导。

I am willing to change the types either in the database, Postgres, or in the Rust code. The database uses numeric and in the Rust code I've tried both f64 and BigDecimal. I am also willing to implement the trait directly myself, but I need some guidance on how to do that as I could not find samples.

推荐答案

柴油使用货运功能进行选择-in来增强功能。

Diesel uses Cargo features to opt-in to enhanced functionality.

我还没有找到清晰的文档页面,但它们在其Cargo.toml

I haven't found a clear documentation page for these, but they are listed in 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版本:

You need to enable the numeric feature and ensure you use a version of bigdecimal that is compatible with Diesel:

[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,
}

另请参见:

  • Why is a trait not implemented for a type that clearly has it implemented?

这篇关于没有为`bigdecimal :: BigDecimal`实现特性`diesel :: Expression`。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆