不满足特征绑定 `chrono::DateTime<Utc>: FromSql<diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>, Pg>`

Bor*_*rsn 2 postgresql rust rust-cargo rust-diesel

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

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

错误:

 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`.
Run Code Online (Sandbox Code Playgroud)

模型.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,
}
Run Code Online (Sandbox Code Playgroud)

/产品/属性.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())
}
Run Code Online (Sandbox Code Playgroud)

使用PgTimestampfromdiesel::pg::data_types::PgTimestamp似乎并不能解决问题。

的数据类型ttimestampz.


尝试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`
Run Code Online (Sandbox Code Playgroud)

kmd*_*eko 6

您收到的错误是抱怨从数据库字段Nullable<Timestampz>DateTime<Utc>. 已经存在从到 的映射,但是如果值为 ,它会做什么?在结构中处理此问题的最佳方法是使用if数据库值为:TimestampzDateTime<Utc>nullPropertyOptionNonenull

pub struct Property {
    pub prod_prop_uuid: Uuid,
    pub t: Option<DateTime<Utc>>, // <-----
    pub empl_user_pvt_uuid: Uuid,
    pub deleted: bool,
}
Run Code Online (Sandbox Code Playgroud)