Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TryFromU64 trait for DateTime<FixedOffset> #330

Closed
kev0960 opened this issue Nov 22, 2021 · 6 comments · Fixed by #331
Closed

Add TryFromU64 trait for DateTime<FixedOffset> #330

kev0960 opened this issue Nov 22, 2021 · 6 comments · Fixed by #331

Comments

@kev0960
Copy link
Contributor

kev0960 commented Nov 22, 2021

The timestamp column (with time zone) generates the field with chrono::DateTime<chrono::FixedOffset>. Sadly, DeriveEntityModel macro fails because TryFromU64 is not implemented for this type.

Example error:

error[E0277]: the trait bound `chrono::DateTime<FixedOffset>: TryFromU64` is not satisfied
  --> src/entity/problem.rs:5:35
   |
5  | #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
   |                                   ^^^^^^^^^^^^^^^^^ the trait `TryFromU64` is not implemented for `chrono::DateTime<FixedOffset>`
   |
   = note: required because of the requirements on the impl of `TryFromU64` for `(i32, chrono::DateTime<FixedOffset>, i32)`
note: required by a bound in `sea_orm::PrimaryKeyTrait::ValueType`
  --> /home/jaebum/.cargo/registry/src/github.com-1ecc6299db9ec823/sea-orm-0.4.0/src/entity/primary_key.rs:49:11
   |
49 |         + TryFromU64;
   |           ^^^^^^^^^^ required by this bound in `sea_orm::PrimaryKeyTrait::ValueType`
   = note: this error originates in the derive macro `DeriveEntityModel` (in Nightly builds, run with -Z macro-backtrace for more info)
@tyt2y3
Copy link
Member

tyt2y3 commented Nov 22, 2021 via email

@billy1624
Copy link
Member

Hi @kev0960, welcome!! Oh, are you using date time column as primary key?

@kev0960
Copy link
Contributor Author

kev0960 commented Nov 22, 2021

Yes :) Postgres Schema looks like this

CREATE TABLE IF NOT EXISTS problem (
  # ...
  version_id TIMESTAMP with time zone NOT NULL DEFAULT current_timestamp,
);

@kev0960
Copy link
Contributor Author

kev0960 commented Nov 22, 2021

I created a pull request here: #331

@kev0960
Copy link
Contributor Author

kev0960 commented Nov 22, 2021

@tyt2y3 Entity file looks like this

//! SeaORM Entity. Generated by sea-orm-codegen 0.3.2

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "problem")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub problem_id: i32,
    #[sea_orm(primary_key, auto_increment = false)]
    pub version_id: DateTimeWithTimeZone,
    #[sea_orm(primary_key, auto_increment = false)]
    pub author_id: i32,
    #[sea_orm(column_type = "Text")]
    pub problem_content: String,
    #[sea_orm(column_type = "Text")]
    pub answer: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::user_info::Entity",
        from = "Column::AuthorId",
        to = "super::user_info::Column::UserId",
        on_update = "NoAction",
        on_delete = "Cascade"
    )]
    UserInfo,
    #[sea_orm(
        belongs_to = "super::problem_metadata::Entity",
        from = "Column::ProblemId",
        to = "super::problem_metadata::Column::ProblemId",
        on_update = "NoAction",
        on_delete = "Cascade"
    )]
    ProblemMetadata,
}

impl Related<super::user_info::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::UserInfo.def()
    }
}

impl Related<super::problem_metadata::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::ProblemMetadata.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}

@tyt2y3
Copy link
Member

tyt2y3 commented Nov 22, 2021

Oh I see, a composite primary key

@billy1624 billy1624 linked a pull request Nov 23, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants