Home > other >  Trait bound `chrono::DateTime<Utc>: FromSql<diesel::sql_types::Nullable<diesel::sql_type
Trait bound `chrono::DateTime<Utc>: FromSql<diesel::sql_types::Nullable<diesel::sql_type

Time:09-08

I am stuck at this error and not sure how to fix it.

Any thoughts what I'm doing wrong?

Error:

 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,
}

/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())
}

Using PgTimestamp from diesel::pg::data_types::PgTimestamp doesn't seem to solve the problem.

The data type of t is timestampz.


Tried diesel::pg::types::sql_types::Timestamptz and Nullable

Getting error:

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`

CodePudding user response:

The error you're getting is complaining about the mapping from a database field that is Nullable<Timestampz> to a DateTime<Utc>. There already exists a mapping from Timestampz to DateTime<Utc>, but what would it do if the value is null? The best way to handle this in your Property struct is to use an Option which will be None if the database value is null:

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