skip to Main Content

I’m getting the following error when trying to compile my rust diesel project despite the diesel docs showing that this trait is implemented

the trait bound DateTime<Utc>: FromSql<diesel::sql_types::Timestamptz, Pg> is not satisfied

My schema.rs

pub mod offers {
    diesel::table! {
        offers.offers (id) {
            id -> Int4,
            #[max_length = 255]
            offername -> Varchar,
            #[max_length = 255]
            offertypeid -> Nullable<Varchar>,
            startdate -> Nullable<Timestamptz>,
            enddate -> Nullable<Timestamptz>,
            frequency -> Nullable<Int4>,
            #[max_length = 255]
            createdby -> Nullable<Varchar>,
            createdAt -> Nullable<Timestamptz>
        }
    }
}

Here is my models.rs:

use diesel::prelude::*;
use chrono::{DateTime, Utc};
use crate::schema::offers::offers as offerTable;

#[derive(Queryable, Selectable)]
#[diesel(table_name = offerTable)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct Offer {
    pub id: i32,
    pub enddate: Option<DateTime<Utc>>,
    pub createdAt: Option<DateTime<Utc>>,
    pub createdby: Option<String>,
    pub frequency: Option<i32>,
    pub offername: String,
    pub startdate: Option<DateTime<Utc>> <--- ERROR FOR THIS LINE
}

In the docs for diesel here is shows that this mapping should work and I haven’t been able to figure out why it’s not working
enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    See @kmdreko's comment for solution


  2. You need to enable the "chrono" feature to enable the implementation for DateTime<UTC> from the chrono crate. This is shown as an annotation in the docs and is not enabled by default. You can read more about this feature and others in Diesel’s crate feature flags section of the docs.

    So your Cargo.toml should contain at least this:

    [dependencies]
    diesel = { version = "2.1.0", features = ["postgres", "chrono"] }
    

    This goes for Diesel version 1.x as well.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search