Repository: diesel-rs/diesel
File: diesel_derives/src/queryable_by_name.rs (Rust)

Code before:
        .map(|f| {
            if f.has_flag("embed") {
                let field_ty = &f.ty;
                Ok(quote!(<#field_ty as QueryableByName<__DB>>::build(
                    row,
                )?))
            } else {
                let field_ty = &f.ty;
                let deserialize_ty = f.ty_for_deserialize()?;
                if model.has_table_name_attribute() {
                    let name = f.column_name_ident();
                    Ok(quote!(
                        {
                            let field = diesel::row::NamedRow::get(row, stringify!(#name))?;
                            <#deserialize_ty as Into<#field_ty>>::into(field)
                        }
                    ))
                } else {
                    let name = f.column_name_str();
                    Ok(quote!(
                       {
                           let field = diesel::row::NamedRow::get(row, #name)?;
                           <#deserialize_ty as Into<#field_ty>>::into(field)
                       }
                    ))
                }
            }
        })
        .collect::<Result<Vec<_>, Diagnostic>>()?;

    let (_, ty_generics, ..) = item.generics.split_for_impl();
    let mut generics = item.generics.clone();
    generics
        .params
        .push(parse_quote!(__DB: diesel::backend::Backend));

    for field in model.fields() {
        let where_clause = generics.where_clause.get_or_insert(parse_quote!(where));
        let field_ty = field.ty_for_deserialize()?;
        if field.has_flag("embed") {
            where_clause
                .predicates
                .push(parse_quote!(#field_ty: QueryableByName<__DB>));
        } else {
            let st = sql_type(field, &model);
            where_clause
                .predicates
                .push(parse_quote!(#field_ty: diesel::deserialize::FromSql<#st, __DB>));
        }
    }


Review comment (suggestion):
I don't think we need to check if there is a `table_name` attribute or not. If there is a `column_name` attribute we should just unconditionally use the string value from there:

```suggestion
                let name = f.column_name_str();
                Ok(quote!(
                   {
                       let field = diesel::row::NamedRow::get(row, #name)?;
                       <#deserialize_ty as Into<#field_ty>>::into(field)
                   }
                ))
```



Code after:
        .map(|f| {
            if f.has_flag("embed") {
                let field_ty = &f.ty;
                Ok(quote!(<#field_ty as QueryableByName<__DB>>::build(
                    row,
                )?))
            } else {
                let field_ty = &f.ty;
                let deserialize_ty = f.ty_for_deserialize()?;
                let name = f.column_name_str();
                Ok(quote!(
                   {
                       let field = diesel::row::NamedRow::get(row, #name)?;
                       <#deserialize_ty as Into<#field_ty>>::into(field)
                   }
                ))
            }
        })
        .collect::<Result<Vec<_>, Diagnostic>>()?;

    let (_, ty_generics, ..) = item.generics.split_for_impl();
    let mut generics = item.generics.clone();
    generics
        .params
        .push(parse_quote!(__DB: diesel::backend::Backend));

    for field in model.fields() {
        let where_clause = generics.where_clause.get_or_insert(parse_quote!(where));
        let field_ty = field.ty_for_deserialize()?;
        if field.has_flag("embed") {
            where_clause
                .predicates
                .push(parse_quote!(#field_ty: QueryableByName<__DB>));
        } else {
            let st = sql_type(field, &model);
            where_clause
                .predicates
                .push(parse_quote!(#field_ty: diesel::deserialize::FromSql<#st, __DB>));
        }
    }

    let (impl_generics, _, where_clause) = generics.split_for_impl();

    Ok(wrap_in_dummy_mod(quote! {
        use diesel::deserialize::{self, QueryableByName};
        use diesel::row::{NamedRow};
        use diesel::sql_types::Untyped;

        impl #impl_generics QueryableByName<__DB>
            for #struct_name #ty_generics
        #where_clause