blackbeam / rust-mysql-simple

Mysql client library implemented in rust.
Apache License 2.0
658 stars 144 forks source link

How to work with chrono? migrate 18.2 -> 22.2 #330

Closed fragsalat closed 2 years ago

fragsalat commented 2 years ago

Hey there,

sorry for the probably dump question. I want to update my dependencies and don't know how to use the library with chrono anymore. The examples and docs is quite limited here. My code looks like this

let query = r#"
    SELECT id, activationCode, createdAt
    FROM user
    WHERE id = ?
"#;

get_connection()
    .exec_first::<(i64, Option<String>, Option<NaiveDateTime>), _, _>(query, (user_id,))
    .map_err(server_error!("Sql query failed to get user"))
    .unwrap_or(None)
    .map(|(id, activation_code, created_at)| User {
        id,
        activated: activation_code.unwrap_or("0".to_owned()) == "0",
        created_at: premium_until.map(|date| DateTime::from_utc(date, Utc)),
    })

In version 18.2 chrono feature were still part of the mysql crate and this code worked. With the new version I added mysql_common and enabled chrono feature but the NaiveDateTime doesn't seem to implement anymore the FromRow trait.

error[E0277]: the trait bound `(i64, std::option::Option<std::string::String>, std::option::Option<NaiveDateTime>): mysql::prelude::FromRow` is not satisfied
   --> db/src/model/user.rs:24:14
    |
24  |             .exec_first::<(i64, Option<String>, Option<NaiveDateTime>), _, _>(query, (user_id,))
    |              ^^^^^^^^^^ the trait `mysql::prelude::FromRow` is not implemented for `(i64, std::option::Option<std::string::String>, std::option::Option<NaiveDateTime>)`
mysql = "22.2.0"
mysql_common = { version = "0.29.0", features = ["chrono"] }

Is there a new way to query data? I'm missing a bit of examples here. Thanks for your work and your help :)

blackbeam commented 2 years ago

Hi.

Is there a new way to query data? I'm missing a bit of examples here.

Please note, that mysql v22.2.0 is not yet updated to mysql_common v0.29.0. This means that you need to change your Cargo.toml to this:

mysql = "22.2.0"
mysql_common = { version = "0.28.2", features = ["chrono"] }
fragsalat commented 2 years ago

seem to have solved it, dump me. May you could add to the documentation / readme that mysql_common has to be used and which version it should be :)