4
votes

I am new to rust and diesel. And trying to create a small demo api using rocket framework.
Getting error: the trait bound NaiveDateTime: Deserialize<'_> is not satisfied

I googled and found some useful links like here : https://github.com/serde-rs/serde/issues/759
It look like some problem with version.

Here are my files:
schema.rs

table! {
    department (dept_id) {
        dept_id -> Int4,
        dept_name -> Nullable<Text>,
        created_on -> Nullable<Timestamp>,
        created_by -> Nullable<Text>,
        modified_on -> Nullable<Timestamp>,
        modified_by -> Nullable<Text>,
        is_active -> Nullable<Bool>,
    }
}

cargo.toml

[dependencies]
diesel = { version = "1.4.5", features = ["postgres","chrono","numeric"] }
dotenv = "0.15.0"
chrono = { version = "0.4.19" }
bigdecimal = { version = "0.1.0" }
rocket = "0.4.6"
rocket_codegen = "0.4.6"
r2d2-diesel = "1.0.0"
r2d2 = "0.8.9"
serde = { version = "1.0.118", features = ["derive"] }
serde_derive = "1.0.118"
serde_json = "1.0.60"

[dependencies.rocket_contrib]
version = "*"
default-features = false
features = ["json"]

model.rs

#![allow(unused)]
#![allow(clippy::all)]

use super::schema::department;
use serde::Serialize;
use serde::Deserialize;

use chrono::NaiveDateTime;
use bigdecimal::BigDecimal;
#[derive(Queryable, Debug, Identifiable, Serialize, Deserialize)]
#[primary_key(dept_id)]
#[table_name = "department"]
pub struct Department {
    pub dept_id: i32,
    pub dept_name: Option<String>,
    pub created_on: Option<NaiveDateTime>,
    pub created_by: Option<String>,
    pub modified_on: Option<NaiveDateTime>,
    pub modified_by: Option<String>,
    pub is_active: Option<bool>,
}

main.rs

#[macro_use]
extern crate diesel;
extern crate dotenv;
extern crate serde;
extern crate serde_derive;
extern crate serde_json;
extern crate chrono;
extern crate bigdecimal;

mod models;
mod schema;
mod connection;

fn main() {
    println!("Hello, Home!");
}

Can anybody help me on this ?
Thanks !

1

1 Answers

5
votes

You need to include serde feature in chrono in your Cargo.toml:

chrono = { version = "0.4", features = ["serde"]}