我正在尝试创建一个可以在柴油中用于插入的结构。具体来说,我将结构插入。编译时出现此错误。
我有一个结构,试图通过derived属性来制作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() {}
我将结构保存在自己的文件中,这是完整的代码。结构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代码中,我尝试了f64
和BigDecimal
。我也愿意直接自己实现该特性,但由于找不到示例,因此我需要一些有关如何实现该特性的指导。
答案 0 :(得分:1)
您可能会通过搜索与以下内容非常相似的错误消息来解决此问题:
the trait `diesel::Expression` is not implemented for `(schema::..., schema::..., schema::...)`
这仅是提醒您计算table!{}
宏中的列数,因为您可能已达到默认的32列限制。
根据Diesel changelog,如果您使用的内容多于默认32列,则应使用以下功能。
diesel = { version = "1.4", features = [..., "64-column-tables"] }
diesel = { version = "1.4", features = [..., "128-column-tables"] }
无法使用更多列,这可能暗示表设计不理想(请参阅:https://github.com/diesel-rs/diesel/issues/2312#issuecomment-591623303)。
答案 1 :(得分:0)
柴油机使用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,
}
另请参阅: