I have a struct that contains a date and I use it with sqlx to retrieve data from my database. So something like:
use sqlx::types::chrono::{DateTime, Utc};
pub struct Account {
pub id: i32,
pub primary_email_id: i32,
pub created: DateTime<Utc>,
}
and
sqlx::query_as!(Account, "select * ...")
This works fine so far. But I also want Account
to be serializable via serde. The obvious approach is:
#[derive(Serialize)]
pub struct Account {
...
This fails, because the Serialize
trait is not implemented for DateTime<Utc>
. I tried the same with PrimitiveDateTime
from the time
crate with the same result. In theory both should support serde
as a feature.
I tried to explicitly add time
or chrono
as dependency, to enable serde
as feature and use the type without the sqlx::types
prefix. But in that case it fails because some sqlx
traits are not implemented.
I assume that I somehow have to enable the serde feature for the classes brought in by sqlx, but I have no idea how to specify a feature for a feature!?
How to I tell sqlx to enable serde for the time/chrono types?