我被这个错误困住了,不知道该如何修正它。
有什么想法我做错了吗?
错误:
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`.
models.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,
}
/产品/特性
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
的diesel::pg::data_types::PgTimestamp
似乎不能解决这个问题。
t
的数据类型是timestampz
。
尝试过diesel::pg::types::sql_types::Timestamptz
和Nullable
获取错误:
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`
发布于 2022-09-06 19:19:37
您所遇到的错误是抱怨从数据库字段(即Nullable<Timestampz>
)到DateTime<Utc>
的映射。从已经存在一个映射到Timestampz
到DateTime<Utc>
,但是如果值是null
,它会做什么?在Property
结构中处理此问题的最佳方法是使用Option
,如果数据库值为null
,则为None
。
pub struct Property {
pub prod_prop_uuid: Uuid,
pub t: Option<DateTime<Utc>>, // <-----
pub empl_user_pvt_uuid: Uuid,
pub deleted: bool,
}
https://stackoverflow.com/questions/73626570
复制相似问题