gpt4 book ai didi

postgresql - 不满足特征绑定(bind) `chrono::DateTime: FromSql, Pg>`

转载 作者:行者123 更新时间:2023-12-03 08:02:18 27 4
gpt4 key购买 nike

我陷入了这个错误,不知道如何修复它。

有什么想法我做错了什么吗?

错误:

 Compiling actix-test v0.1.0 (/Users/b/o/d/Rust/actix-test)
error[E0277]: the trait bound `chrono::DateTime<Utc>: FromSql<diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>, Pg>` is not satisfied
--> src/app/e/api/products/properties.rs:16:61
|
16 | let query_result = web::block(move || properties::table.load::<Property>(&*con).unwrap()).await;
| ^^^^ the trait `FromSql<diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>, Pg>` is not implemented for `chrono::DateTime<Utc>`
|
= help: the following implementations were found:
<chrono::DateTime<Utc> as FromSql<diesel::sql_types::Timestamptz, Pg>>
= note: required because of the requirements on the impl of `Queryable<diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>, Pg>` for `chrono::DateTime<Utc>`
= note: 2 redundant requirements hidden
= note: required because of the requirements on the impl of `Queryable<(diesel::sql_types::Uuid, diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>, diesel::sql_types::Uuid, diesel::sql_types::Bool, diesel::sql_types::Uuid, diesel::sql_types::Nullable<Integer>, diesel::sql_types::Nullable<Integer>, diesel::sql_types::Nullable<Integer>, diesel::sql_types::Nullable<Integer>, diesel::sql_types::Nullable<Integer>, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Numeric, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Bool), Pg>` for `Property`
= note: required because of the requirements on the impl of `LoadQuery<_, Property>` for `schemas::products::properties::table`
note: required by a bound in `load`
--> /Users/b/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-1.4.8/src/query_dsl/mod.rs:1238:15
|
1238 | Self: LoadQuery<Conn, U>,
| ^^^^^^^^^^^^^^^^^^ required by this bound in `load`

For more information about this error, try `rustc --explain E0277`.

模型.rs

use bigdecimal::BigDecimal;
use chrono::{DateTime, Utc};
use uuid::Uuid;


#[allow(unused)]
#[derive(Queryable, serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct Property {
pub prod_prop_uuid: Uuid,
pub t: DateTime<Utc>,
pub empl_user_pvt_uuid: Uuid,
pub deleted: bool,
}

/product/properties.rs

use crate::app::db::connection::DbPool;
use actix_web::{get, web, HttpResponse, Responder};
use diesel::prelude::*;

#[get("/product/properties")]
pub async fn list(db_pool: web::Data<DbPool>) -> impl Responder {
use crate::app::db::models::products::Property;
use crate::app::db::schemas::products::properties;

let con_result = db_pool.get();
if let Err(e) = con_result {
return HttpResponse::InternalServerError().body(format!("{:?}", e));
}

let con = con_result.unwrap();
let query_result = web::block(move || properties::table.load::<Property>(&*con).unwrap()).await;
if let Err(e) = query_result {
return HttpResponse::InternalServerError().body(format!("{:?}", e));
}

HttpResponse::Ok().json(query_result.unwrap())
}

使用 diesel::pg::data_types::PgTimestamp 中的 PgTimestamp 似乎无法解决问题。

t的数据类型是timestampz


尝试了diesel::pg::types::sql_types::TimestamptzNullable

出现错误:

error[E0277]: the trait bound `diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>: Queryable<diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>, Pg>` is not satisfied
--> src/app/e/api/products/properties.rs:16:61
|
16 | let query_result = web::block(move || properties::table.load::<Property>(&*con).unwrap()).await;
| ^^^^ the trait `Queryable<diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>, Pg>` is not implemented for `diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>`
|
= note: required because of the requirements on the impl of `Queryable<(diesel::sql_types::Uuid, diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>, diesel::sql_types::Uuid, diesel::sql_types::Bool, diesel::sql_types::Uuid, diesel::sql_types::Nullable<Integer>, diesel::sql_types::Nullable<Integer>, diesel::sql_types::Nullable<Integer>, diesel::sql_types::Nullable<Integer>, diesel::sql_types::Nullable<Integer>, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Numeric, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Bool), Pg>` for `(uuid::Uuid, diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>, uuid::Uuid, bool, uuid::Uuid, std::option::Option<i32>, std::option::Option<i32>, std::option::Option<i32>, std::option::Option<i32>, std::option::Option<i32>, std::option::Option<BigDecimal>, BigDecimal, std::option::Option<BigDecimal>, std::option::Option<BigDecimal>, std::option::Option<BigDecimal>, std::option::Option<BigDecimal>, std::option::Option<BigDecimal>, bool)`
= note: 1 redundant requirement hidden
= note: required because of the requirements on the impl of `Queryable<(diesel::sql_types::Uuid, diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>, diesel::sql_types::Uuid, diesel::sql_types::Bool, diesel::sql_types::Uuid, diesel::sql_types::Nullable<Integer>, diesel::sql_types::Nullable<Integer>, diesel::sql_types::Nullable<Integer>, diesel::sql_types::Nullable<Integer>, diesel::sql_types::Nullable<Integer>, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Numeric, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Nullable<diesel::sql_types::Numeric>, diesel::sql_types::Bool), Pg>` for `Property`
= note: required because of the requirements on the impl of `LoadQuery<_, Property>` for `schemas::products::properties::table`
note: required by a bound in `load`
--> /Users/b/.cargo/registry/src/github.com-1ecc6299db9ec823/diesel-1.4.8/src/query_dsl/mod.rs:1238:15
|
1238 | Self: LoadQuery<Conn, U>,
| ^^^^^^^^^^^^^^^^^^ required by this bound in `load`

error[E0277]: the trait bound `diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>: serde::Serialize` is not satisfied
--> src/app/db/models/products.rs:15:5
|
15 | pub t: Nullable<Timestamptz>,
| ^^^ the trait `serde::Serialize` is not implemented for `diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>`
|
note: required by a bound in `serde::ser::SerializeStruct::serialize_field`
--> /Users/b/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.137/src/ser/mod.rs:1899:12
|
1899 | T: Serialize;
| ^^^^^^^^^ required by this bound in `serde::ser::SerializeStruct::serialize_field`

error[E0277]: the trait bound `diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>: serde::Deserialize<'_>` is not satisfied
--> src/app/db/models/products.rs:15:5
|
15 | pub t: Nullable<Timestamptz>,
| ^^^ the trait `serde::Deserialize<'_>` is not implemented for `diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>`
|
note: required by a bound in `next_element`
--> /Users/b/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.137/src/de/mod.rs:1725:12
|
1725 | T: Deserialize<'de>,
| ^^^^^^^^^^^^^^^^ required by this bound in `next_element`

error[E0277]: the trait bound `diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>: serde::Deserialize<'_>` is not satisfied
--> src/app/db/models/products.rs:15:5
|
15 | pub t: Nullable<Timestamptz>,
| ^^^ the trait `serde::Deserialize<'_>` is not implemented for `diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>`
|
note: required by a bound in `next_value`
--> /Users/b/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.137/src/de/mod.rs:1864:12
|
1864 | V: Deserialize<'de>,
| ^^^^^^^^^^^^^^^^ required by this bound in `next_value`

最佳答案

您收到的错误是提示数据库字段 Nullable<Timestampz> 的映射。到 DateTime<Utc> 。有already exists a mapping来自TimestampzDateTime<Utc> ,但是如果值为 null 会做什么? ?处理这个问题的最好方法是 Property struct 是使用 Option这将是None如果数据库值为 null :

pub struct Property {
pub prod_prop_uuid: Uuid,
pub t: Option<DateTime<Utc>>, // <-----
pub empl_user_pvt_uuid: Uuid,
pub deleted: bool,
}

关于postgresql - 不满足特征绑定(bind) `chrono::DateTime<Utc>: FromSql<diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>, Pg>`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73626570/

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