I have a problem in my code for creating a table in rocket with serde and chrono with datetime
Here is the error :
error[E0277]: the trait bound `chrono::datetime::DateTime<chrono::offset::utc::Utc>: diesel::Expression` is not satisfied
--> src/models/task_model.rs:26:10
|
26 | #[derive(Insertable, Deserialize)]
| ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `chrono::datetime::DateTime<chrono::offset::utc::Utc>`
|
= note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Timestamp>` for `chrono::datetime::DateTime<chrono::offset::utc::Utc>`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `chrono::datetime::DateTime<chrono::offset::utc::Utc>: diesel::Expression` is not satisfied
--> src/models/task_model.rs:26:10
|
26 | #[derive(Insertable, Deserialize)]
| ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `chrono::datetime::DateTime<chrono::offset::utc::Utc>`
|
= note: required because of the requirements on the impl of `diesel::Expression` for `&chrono::datetime::DateTime<chrono::offset::utc::Utc>`
= note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Timestamp>` for `&chrono::datetime::DateTime<chrono::offset::utc::Utc>`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `chrono::datetime::DateTime<chrono::offset::utc::Utc>: diesel::Expression` is not satisfied
--> src/models/task_model.rs:26:10
|
26 | #[derive(Insertable, Deserialize)]
| ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `chrono::datetime::DateTime<chrono::offset::utc::Utc>`
|
= note: required because of the requirements on the impl of `diesel::Expression` for `&'insert chrono::datetime::DateTime<chrono::offset::utc::Utc>`
= note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Timestamp>` for `&'insert chrono::datetime::DateTime<chrono::offset::utc::Utc>`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 3 previous errors; 7 warnings emitted
task_model.rs
use diesel;
use diesel::prelude::*;
use serde_derive::{Serialize,Deserialize};
use rocket_contrib::json::Json;
use crate::schema::tasks;
use crate::schema::tasks::dsl::tasks as all_tasks;
use crate::DbConn;
use chrono::{DateTime, TimeZone, Utc, NaiveDateTime};
use chrono::serde::ts_seconds;
#[derive(Queryable, Serialize)]
pub struct Task {
pub id: i32,
pub name: String,
pub arguments: String,
pub priority: i32,
pub done: bool,
pub created_at: DateTime<Utc>,
#[serde(default, deserialize_with = "option_datefmt")]
pub start_time : Option<DateTime<Utc>>,
#[serde(default, deserialize_with = "option_datefmt")]
pub end_time : Option<DateTime<Utc>>,
}
#[derive(Insertable, Deserialize)]
#[table_name = "tasks"]
pub struct NewTask {
pub name: String,
pub arguments: String,
pub priority: i32,
pub created_at: DateTime<Utc>,
}
impl Task {
pub fn insert(conn: DbConn, new_task: Json<NewTask>, ) -> bool {
diesel::insert_into(tasks::table)
.values(&new_task)
.get_result(&*conn)
.unwrap()
}
pub fn create(name: String,arguments: String,priority: i32, conn: DbConn) -> bool {
let now = Utc::now().naive_utc();
let new_task = NewTask {
name: name,
arguments: arguments,
priority: priority,
created_at: now
};
diesel::insert_into(tasks::table)
.values(&new_task)
.get_result(&*conn)
.unwrap()
}
}
option_datefmt.rs :
use chrono::{DateTime, TimeZone, Utc};
use serde::{Serialize,Deserialize,Deserializer};
use serde_derive::{Serialize,Deserialize};
const FORMAT: &str = "%Y-%m-%d %H:%M:%S";
fn datefmt<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Utc.datetime_from_str(&s, FORMAT)
.map_err(serde::de::Error::custom)
}
fn option_datefmt<'de, D>(deserializer: D) -> Result<Option<DateTime<Utc>>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Wrapper(#[serde(deserialize_with = "datefmt")] DateTime<Utc>);
let v = Option::deserialize(deserializer)?;
Ok(v.map(|Wrapper(a)| a))
}
Cargo.toml :
[package]
name = "testrocket"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
diesel = { version = "1.4.0", features = ["postgres","chrono"] }
dotenv = "0.10"
chrono = { version = "0.4.11", features = ["serde"] }
serde = "1.0"
rocket = "0.4"
[dependencies.serde_derive]
version = "1.0"
[dependencies.rocket_contrib]
version = "0.4"
default-features = false
features = ["json","diesel_postgres_pool"]
Do you have an idea about how to fix it , please ? I'm stuck