TimestampWithTimeZone returned as array
rgreinho opened this issue · 0 comments
Description & Expected Behavior
In my models I have the created_at/updated_at columns defined as TimestampWithTimeZone
.
When I retrieve the models, the TimestampWithTimeZone
columns are returned as arrays with a strange format, like \"created_at\":[2024,75,16,24,39,144518000,0,0,0]}
but I was expecting an ISO8601, something more like \"created_at\":\"+002024-03-15T16:26:38.178125000Z\"
that my frontend can consume directly.
Reproduces How Often
Always reproducible.
Workarounds
My work around currently is to generate my entities with sea-orm-cli generate entity -o "entity/src/entities" --with-serde both --date-time-crate time
, then to manually edit the models everywhere I use a TimestampWithTimeZone
and add this serde attribute: #[serde(with = "time::serde::iso8601")]
. However this approach does not scale.
Is there a way to have seaorm automatically add the serde attribute for the TimestampWithTimeZone fields?
Reproducible Example
entity/src/entities/submission.rs
:
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14
use super::sea_orm_active_enums::ApprovalStatus;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "submission")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub first_name: String,
pub last_name: String,
pub title: Option<String>,
pub organization: Option<String>,
pub email: String,
pub country: String,
pub city: String,
pub region: Option<String>,
pub fips_code: String,
pub consent: bool,
pub created_at: TimeDateTimeWithTimeZone,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_date_ser() {
let m = submission::Model {
id: 1,
first_name: String::from("first_name"),
last_name: String::from("last_name"),
title: Some(String::from("title")),
organization: Some(String::from("organization")),
email: String::from("email"),
country: String::from("country"),
city: String::from("city"),
region: Some(String::from("region")),
fips_code: String::from("fips_code"),
consent: true,
created_at: TimeDateTimeWithTimeZone::now_utc(),
};
let d = serde_json::to_string(&m).unwrap();
dbg!(d);
}
}
The JSON output is:
{
"id": 1,
"first_name": "first_name",
"last_name": "last_name",
"title": "title",
"organization": "organization",
"email": "email",
"country": "country",
"city": "city",
"region": "region",
"fips_code": "fips_code",
"consent": true,
"status": "Pending",
"created_at": [
2024,
75,
18,
33,
44,
836831000,
0,
0,
0
]
}
Versions
I am using seaorm 0.12.14 with the time library.