How can I instruct sqlx about this custom type I'm using for OffsetDateTime?

I have a custom time type because time's authors doesn't want to add a Default value yet:

#[derive(Debug, Clone, Copy)]
pub struct OffsetDateTime(pub time::OffsetDateTime);

impl OffsetDateTime {
    pub fn now_utc() -> Self {
        Self(time::OffsetDateTime::now_utc())
    }
}

impl Default for OffsetDateTime {
    fn default() -> Self {
        Self(time::OffsetDateTime::UNIX_EPOCH)
    }
}

impl From<time::OffsetDateTime> for OffsetDateTime {
    fn from(o: time::OffsetDateTime) -> Self {
        Self(o)
    }
}

impl From<OffsetDateTime> for time::OffsetDateTime {
    fn from(o: OffsetDateTime) -> Self {
        o.0
    }
}

If I use this simple sqlx code:

#[derive(Debug, Default, sqlx::FromRow)]
pub struct Player {
    pub id: String,
    pub created_at: OffsetDateTime,
    pub name: String,
}

let pla = sqlx::query_as!(
    Player,
    r#"SELECT * from player where id = $1"#,
    '1'
)
.fetch_one(&*self.pool)
.await?;

it errors with:

error[E0308]: mismatched types
   |
66 |           let pla = sqlx::query_as!(
   |  ___________________^
67 | |             Player,
68 | |             r#"SELECT * from player where id = $1"#,
70 | |             id
71 | |         )
   | |_________^ expected struct `custom::types::OffsetDateTime`, found struct `time::OffsetDateTime`
   |
   = note: struct `time::OffsetDateTime` and struct `custom::types::OffsetDateTime` have similar names, but are actually distinct types
note: struct `time::OffsetDateTime` is defined in crate `time`
  --> C:\Users\Fred\.cargo\registry\src\github.com-1ecc6299db9ec823\time-0.3.17\src\offset_date_time.rs:31:1
   |
31 | pub struct OffsetDateTime {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^
note: struct `custom::types::OffsetDateTime` is defined in crate `custom`
   |
4  | pub struct OffsetDateTime(pub time::OffsetDateTime);
   | ^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query_as` (in Nightly builds, run with -Z macro-backtrace for more info)
help: try wrapping the expression in `custom::types::OffsetDateTime`
  --> C:\Users\Fred\.cargo\registry\src\github.com-1ecc6299db9ec823\sqlx-0.6.2\src\macros\mod.rs:561:9
   |
561|         custom::types::OffsetDateTime($crate::sqlx_macros::expand_query!(record = $out_struct, source = $query, args = [$($args)*]))
   |         ++++++++++++++++++++++++++++++                                                                                             +

How can I fix this?

Should I instruct sqlx to handle my custom OffsetDateTime? How?

I haven't done this myself, but looking at the docs of FromRow and of Row, it seems like you'd at least need Decode and Type. And based on those docs, you could try:

#[derive(Debug, Clone, Copy, sqlx::Type)]
#[sqlx(transparent)]
pub struct OffsetDateTime(pub time::OffsetDateTime);

(I'd probably give it a distinct name too, to at least save myself the confusion.)

I tried exactly your code but this is the error:

error[E0308]: mismatched types
   |
61 |           let res = sqlx::query_as!(
   |  ___________________^
62 | |             PgPlayer,
63 | |             r#"SELECT * from player where team_id = $1 AND id = $2"#,
64 | |             team_id,
65 | |             id
66 | |         )
   | |_________^ expected struct `custom::types::OffsetDateTime`, found struct `time::OffsetDateTime`
   |
   = note: struct `time::OffsetDateTime` and struct `custom::types::OffsetDateTime` have similar names, but are actually distinct types
note: struct `time::OffsetDateTime` is defined in crate `time`
  --> C:\Users\Fred\.cargo\registry\src\github.com-1ecc6299db9ec823\time-0.3.17\src\offset_date_time.rs:31:1
   |
31 | pub struct OffsetDateTime {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^
note: struct `custom::types::OffsetDateTime` is defined in crate `custom`
   |
9  | pub struct OffsetDateTime(pub time::OffsetDateTime);
   | ^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query_as` (in Nightly builds, run with -Z macro-backtrace for more info)
help: try wrapping the expression in `custom::types::OffsetDateTime`
  --> C:\Users\Fred\.cargo\registry\src\github.com-1ecc6299db9ec823\sqlx-0.6.2\src\macros\mod.rs:561:9
   |
561|         custom::types::OffsetDateTime($crate::sqlx_macros::expand_query!(record = $out_struct, source = $query, args = [$($args)*]))
   |         ++++++++++++++++++++++++++++++                                                                                             +

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.