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
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:
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.
diesel = { version = "1.4", features = [..., "64-column-tables"] }
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).