the trait `diesel::Expression` is not implemented for `bigdecimal::BigDecimal`

前端 未结 2 2101
广开言路
广开言路 2021-01-12 15:22

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.

I have a struct

2条回答
  •  半阙折子戏
    2021-01-12 15:50

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

    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"]
    

    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"
    

    And the code compiles:

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

    See also:

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

提交回复
热议问题