gpt4 book ai didi

postgresql - 特性 `diesel::Expression` 没有为 `bigdecimal::BigDecimal` 实现

转载 作者:行者123 更新时间:2023-11-29 08:26:32 29 4
gpt4 key购买 nike

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

我有一个结构,我正试图通过 derive 属性使 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() {}

我的结构在它自己的文件中,这是完整的代码。 struct 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、diesel 1.4.2 和 Postgres 11。

我愿意更改数据库、Postgres 或 Rust 代码中的类型。数据库使用 numeric,在 Rust 代码中我尝试了 f64BigDecimal。我也愿意自己直接实现该特征,但由于找不到示例,我需要有关如何实现的一些指导。

最佳答案

柴油使用 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"]

您需要启用numeric 功能并确保您使用与 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,
}

另见:

关于postgresql - 特性 `diesel::Expression` 没有为 `bigdecimal::BigDecimal` 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55783064/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com