10
votes

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

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`

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

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.

2
What purpose does providing the struct Thread serve? Is it required to reproduce the error? Please ensure your minimal reproducible example is minimal.Shepmaster
Please also attempt to format your question in a readable manner. The editor provides UI buttons to help you format it correctly, there is a help document if you are unfamiliar with Markdown, you can look at the existing edits that have been made to your question, and there's a live preview so you know what you are going to submit.Shepmaster
The code you have provided does not report the error you are asking about. Instead: The attribute table_name is currently unknown to the compiler and may have meaning added to it in the future. Please review how to create a minimal reproducible example and then edit your question to include it. Try to reproduce your error in a brand new Cargo project. There are Rust-specific MCVE tips and Diesel tips you can use to reduce your original code for posting here.Shepmaster
Is it required to include all of those dependencies, such as "sparkpost", to produce the error? Are the fields Title, Bounty, etc required? Please ensure your minimal reproducible example is minimal.Shepmaster

2 Answers

17
votes

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:

3
votes

You might reach this question by searching an error message that is really similar:

the trait `diesel::Expression` is not implemented for `(schema::..., schema::..., schema::...)`

This is just a reminder to count the number of columns you have in the table!{} macro because you might be reaching the default 32 column limit.

According to the Diesel changelog you should use the following features if you use more than the default 32 columns.

64 columns (Cargo.toml)

diesel = { version = "1.4", features = [..., "64-column-tables"] }

128 columns (Cargo.toml):

diesel = { version = "1.4", features = [..., "128-column-tables"] }

Using more columns is not possible and might be a hint that the table design is not ideal (See: https://github.com/diesel-rs/diesel/issues/2312#issuecomment-591623303).