diff --git a/sqlx-macros-core/src/derives/attributes.rs b/sqlx-macros-core/src/derives/attributes.rs index bf2a357dfd..bfbc32f60d 100644 --- a/sqlx-macros-core/src/derives/attributes.rs +++ b/sqlx-macros-core/src/derives/attributes.rs @@ -3,7 +3,7 @@ use quote::{quote, quote_spanned}; use syn::punctuated::Punctuated; use syn::spanned::Spanned; use syn::token::Comma; -use syn::{Attribute, DeriveInput, Field, Lit, Meta, MetaNameValue, NestedMeta, Variant}; +use syn::{Attribute, DeriveInput, Field, Lit, Meta, MetaNameValue, NestedMeta, Type, Variant}; macro_rules! assert_attribute { ($e:expr, $err:expr, $input:expr) => { @@ -62,7 +62,7 @@ pub struct SqlxChildAttributes { pub rename: Option, pub default: bool, pub flatten: bool, - pub try_from: Option, + pub try_from: Option, pub skip: bool, } diff --git a/tests/mysql/macros.rs b/tests/mysql/macros.rs index ce8e0d2428..24807d5606 100644 --- a/tests/mysql/macros.rs +++ b/tests/mysql/macros.rs @@ -435,4 +435,37 @@ async fn test_try_from_attr_with_flatten() -> anyhow::Result<()> { Ok(()) } +#[sqlx_macros::test] +async fn test_try_from_attr_with_complex_type() -> anyhow::Result<()> { + mod m { + #[derive(sqlx::Type)] + #[sqlx(transparent)] + pub struct ComplexType(T); + + impl std::convert::TryFrom> for u64 { + type Error = std::num::TryFromIntError; + fn try_from(value: ComplexType) -> Result { + u64::try_from(value.0) + } + } + } + + #[derive(sqlx::FromRow)] + struct Record { + #[sqlx(try_from = "m::ComplexType")] + id: u64, + } + + let mut conn = new::().await?; + let (mut conn, id) = with_test_row(&mut conn).await?; + + let record = sqlx::query_as::<_, Record>("select id from tweet") + .fetch_one(&mut *conn) + .await?; + + assert_eq!(record.id, id.0 as u64); + + Ok(()) +} + // we don't emit bind parameter type-checks for MySQL so testing the overrides is redundant