Skip to content

Model field with chrono::DateTime<chrono::Local> panic #1222

Closed

Description

@Colstuwjx

Hi team,

I've used sqlx with the following model:

CREATE TABLE `user` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `ctime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `mtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `is_deleted` tinyint(4) COLLATE utf8_general_ci NOT NULL DEFAULT 0,
  `email` varchar(255) COLLATE utf8_general_ci NOT NULL DEFAULT '',
  `name` varchar(255) COLLATE utf8_general_ci NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  KEY `ix_mtime` (`mtime`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
...
#[derive(FromRow, Serialize, Deserialize, Debug)]
pub struct User {
    pub id: SqlID,
    pub ctime: chrono::DateTime<Utc>,
    pub mtime: chrono::DateTime<Utc>,
    pub name: String,
    pub email: String,
}
...
let result = sqlx::query_as("SELECT * FROM user")
    .fetch_all(pool.get_ref()).await;

and it compiles with error below:

the trait `sqlx::Type<sqlx::MySql>` is not implemented for `chrono::DateTime<chrono::Local>`

If I changed to chrono::DateTime<chrono::Utc>, it works.

config in Cargo.toml likes below:

sqlx = { version = "0.4.2", features = [ "runtime-actix-native-tls", "mysql", "chrono" ] }
chrono =  { version = "0.4.15", features = ["serde"] }

Help!

Activity

  1. juzi5201314 commented on Jun 13, 2021

    @juzi5201314

    me too!

    error[E0277]: the trait bound `DateTime<Local>: types::Type<MySql>` is not satisfied
       --> C:\Users\10342\.cargo\registry\src\github.com-1ecc6299db9ec823\sqlx-core-0.5.5\src\any\type.rs:24:25
        |
    24  |                         <$ty as crate::types::Type<crate::mysql::MySql>>::compatible(&ty)
        |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `types::Type<MySql>` is not implemented for `DateTime<Local>`
        | 
       ::: C:\Users\10342\.cargo\registry\src\github.com-1ecc6299db9ec823\sqlx-core-0.5.5\src\types\mod.rs:177:5
        |
    177 |     fn compatible(ty: &DB::TypeInfo) -> bool {
        |     ---------------------------------------- required by `types::Type::compatible`
        | 
       ::: C:\Users\10342\.cargo\registry\src\github.com-1ecc6299db9ec823\sqlx-core-0.5.5\src\any\types.rs:127:1
        |
    127 | impl_any_type!(chrono::DateTime<chrono::offset::Local>);
        | -------------------------------------------------------- in this macro invocation
        |
        = help: the following implementations were found:
                  <DateTime<Local> as types::Type<any::database::Any>>
                  <DateTime<Tz> as types::Type<Sqlite>>
                  <DateTime<Tz> as types::Type<postgres::database::Postgres>>
                  <DateTime<Utc> as types::Type<MySql>>
                  <DateTime<Utc> as types::Type<any::database::Any>>
        = note: this error originates in the macro `impl_any_type` (in Nightly builds, run with -Z macro-backtrace for more info)
    
    
  2. jplatte commented on Jun 13, 2021

    @jplatte
    Contributor

    Have you searched for existing (closed) issues? I'm pretty sure this has been discussed multiple times already (though might also have been on Discord only).

  3. juzi5201314 commented on Jun 14, 2021

    @juzi5201314

    您是否搜索过现有(已关闭)的问题?我很确定这已经被讨论过多次(尽管也可能只在 Discord 上讨论过)。

    emm, I searched for issues related to chrono, and only found this #1224... but the problem remains...

  4. jplatte commented on Jun 14, 2021

    @jplatte
    Contributor

    Right, that's the only reference I can find too. I thought I had seen pretty much this exact issue before.

    Actually I can't find Type implementations for DateTime<Local> for any database, only Decode impls for Postgres and SQLite. The files these are in is sqlx-core/src/{db}/types/chrono.rs. Shouldn't be too hard to adapt the code from postgres / sqlite to mysql and also add Type impls.

  5. abonander commented on Jul 21, 2021

    @abonander
    Collaborator

    This is also causing CI errors for the MySQL passes so I'm going to go ahead and fix this.

  6. added 3 commits that reference this issue on Jul 21, 2021
    f7c881a
    a2aa1dc
    2ab1c9a
  7. added a commit that references this issue on Jul 21, 2021
    8bcac03
  8. added 2 commits that reference this issue on Nov 19, 2021
    6cdb35d
    b5a95c4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions