首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >性状绑定的FromSql<diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>,`chrono::DateTime<Utc>:Pg>`不满意

性状绑定的FromSql<diesel::sql_types::Nullable<diesel::sql_types::Timestamptz>,`chrono::DateTime<Utc>:Pg>`不满意
EN

Stack Overflow用户
提问于 2022-09-06 18:47:07
回答 1查看 299关注 0票数 0

我被这个错误困住了,不知道该如何修正它。

有什么想法我做错了吗?

错误:

代码语言:javascript
运行
复制
 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

代码语言:javascript
运行
复制
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,
}

/产品/特性

代码语言:javascript
运行
复制
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::PgTimestampdiesel::pg::data_types::PgTimestamp似乎不能解决这个问题。

t的数据类型是timestampz

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

获取错误:

代码语言:javascript
运行
复制
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`
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-06 19:19:37

您所遇到的错误是抱怨从数据库字段(即Nullable<Timestampz> )到DateTime<Utc>的映射。从已经存在一个映射TimestampzDateTime<Utc>,但是如果值是null,它会做什么?在Property结构中处理此问题的最佳方法是使用Option,如果数据库值为null,则为None

代码语言:javascript
运行
复制
pub struct Property {
    pub prod_prop_uuid: Uuid,
    pub t: Option<DateTime<Utc>>, // <-----
    pub empl_user_pvt_uuid: Uuid,
    pub deleted: bool,
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73626570

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档