diff --git a/sqlx-macros-core/src/query/output.rs b/sqlx-macros-core/src/query/output.rs index 987dcaa3cb..47320c6272 100644 --- a/sqlx-macros-core/src/query/output.rs +++ b/sqlx-macros-core/src/query/output.rs @@ -146,18 +146,27 @@ pub fn quote_query_as( |( i, RustColumn { - var_name, type_, .. + var_name, + type_, + ident, }, )| { match (input.checked, type_) { // we guarantee the type is valid so we can skip the runtime check - (true, ColumnType::Exact(type_)) => quote! { - // binding to a `let` avoids confusing errors about - // "try expression alternatives have incompatible types" - // it doesn't seem to hurt inference in the other branches - #[allow(non_snake_case)] - let #var_name = row.try_get_unchecked::<#type_, _>(#i)?.into(); - }, + (true, ColumnType::Exact(type_)) => { + let column_ident = quote::format_ident!("{}", ident); + + quote! { + // binding to a `let` avoids confusing errors about + // "try expression alternatives have incompatible types" + // it doesn't seem to hurt inference in the other branches + #[allow(non_snake_case)] + let #var_name = row.try_get_unchecked::<#type_, _>( + // use the column identity as the index, + stringify!(#column_ident) + )?.into(); + } + } // type was overridden to be a wildcard so we fallback to the runtime check (true, ColumnType::Wildcard) => quote! ( #[allow(non_snake_case)]