SeaQL / sea-orm

🐚 An async & dynamic ORM for Rust
https://www.sea-ql.org/SeaORM/
Apache License 2.0
7.3k stars 513 forks source link

`to_string` seems to be broken for `sea_orm::query::Statement` on `OffsetDatetime` #1096

Closed saintazunya closed 2 years ago

saintazunya commented 2 years ago

When a field is type OffsetDatetime, to_string method generates a truncated time instead of full precision.

quick example:

use sea_orm::{DbBackend, Set};
use sea_orm::entity::prelude::*;
use time::OffsetDateTime;

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "testtable")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    id: i64,
    created_at: OffsetDateTime
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

#[tokio::main]
async fn main()  {
    let model = ActiveModel {
        id: Set(1),
        created_at: Set(OffsetDateTime::now_utc())
    };
    let sql = Entity::insert(model).build(DbBackend::Postgres).to_string();
    eprintln!("{}", sql);
}

prints out INSERT INTO "testtable" ("id", "created_at") VALUES (1, '2022-10-07 03:50:22 +0000')

while expecting: INSERT INTO "testtable" ("id", "created_at") VALUES (1, '2022-10-07 03:50:22.767233 +00:00')

billy1624 commented 2 years ago

Hey @saintazunya, this is just a display issue. In reality the OffsetDateTime is binded (insert) with subseconds.

let transaction_log = transaction_log::Model {
    id: 1,
    date: date!(2022 - 03 - 13),
    time: time!(16:24:00),
    date_time: date!(2022 - 03 - 13).with_time(time!(16:24:00)),
    date_time_tz: date!(2022 - 03 - 13)
        .with_time(time!(16:24:00.123456789))
        .assume_utc(),
};

let res = TransactionLog::insert(transaction_log.clone().into_active_model())
    .exec(db)
    .await?;
image

The to_string method is defined in SeaQuery at here whereas the to_string format is defined as "[year]-[month]-[day] [hour]:[minute]:[second] [offset_hour sign:mandatory][offset_minute]" at here

saintazunya commented 2 years ago

Hi @billy1624 thanks for replying.

I understand this is a display issue but I think to_string should also return a valid, as expected query instead of truncating the details in datatime. Or at lease we should add a documentation on the method to indicate its behaviors.

tyt2y3 commented 2 years ago

Right, a PR on sea-query would be appreciated to fix the formatting.

ikrivosheev commented 2 years ago

Right, a PR on sea-query would be appreciated to fix the formatting.

@tyt2y3 hello! This is breaching change, is it ok?

tyt2y3 commented 2 years ago

Yes

ikrivosheev commented 2 years ago

@saintazunya thank you, I create PR: https://github.com/SeaQL/sea-query/pull/468