{"record":{"id":"97d419a1feb72bf1","repo":"diesel-rs/diesel","slug":"references-are-not-supported-in-queryable-types","errorCode":null,"errorMessage":"references are not supported in `Queryable` types\nconsider using `std::borrow::Cow<'{}, {}>` instead","messagePattern":"references are not supported in `Queryable` types\nconsider using `std::borrow::Cow<'(.+?), (.+?)>` instead","errorType":"exception","errorClass":"syn::Error","httpStatus":null,"severity":"error","filePath":"diesel_derives/src/selectable.rs","lineNumber":150,"sourceCode":"    let where_clause = where_clause.get_or_insert_with(|| parse_quote!(where));\n    for field_check in field_check_bound {\n        where_clause.predicates.push(field_check);\n    }\n    Ok(quote::quote! {\n        fn #function_name #original_impl_generics()\n            #where_clause\n        {}\n    })\n}\n\nfn to_field_ty_bound(field_ty: &syn::Type) -> Result<TokenStream> {\n    match field_ty {\n        syn::Type::Reference(r) => {\n            use crate::quote::ToTokens;\n            // references are not supported for checking for now\n            //\n            // (How ever you can even have references in a `Queryable` struct anyway)\n            Err(syn::Error::new(\n                field_ty.span(),\n                format!(\n                    \"references are not supported in `Queryable` types\\n\\\n                         consider using `std::borrow::Cow<'{}, {}>` instead\",\n                    r.lifetime\n                        .as_ref()\n                        .expect(\"It's a struct field so it must have a named lifetime\")\n                        .ident,\n                    r.elem.to_token_stream()\n                ),\n            ))\n        }\n        field_ty => Ok(quote::quote! {\n            #field_ty\n        }),\n    }\n}\n","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/diesel-rs/diesel/blob/6fa6ed01b24b24248ab2a611698d0a7c6a2e9120/diesel_derives/src/selectable.rs#L132-L168","documentation":"The `#[derive(Queryable)]` proc macro in diesel_derives generates code that checks each field type of the struct against the query row. Fields declared as references (e.g. `&'a str`) cannot be supported because generated deserialization code cannot produce borrows with valid lifetimes from the row. The macro explicitly rejects `syn::Type::Reference` fields in `to_field_ty_bound` and suggests `std::borrow::Cow` as a replacement that can own or borrow data.","triggerScenarios":"Deriving `Queryable` (or `QueryableByName`/`Selectable` checked via `generate_check_function`) on a struct containing a reference-typed field such as `name: &'a str` or `&'a [u8]`.","commonSituations":"Developers porting structs that borrowed from input data, trying to avoid allocations in query results, or copying a struct shape that works with serde but not with diesel's `Queryable` derive.","solutions":["Change the reference field to an owned type (`String`, `Vec<u8>`) or `std::borrow::Cow<'a, str>`.","If you must avoid allocation, deserialize to owned types and borrow afterwards, or use `sql_types::Text` mappings with owned `String`.","If the reference is unnecessary, drop the lifetime entirely and use owned field types."],"exampleFix":"// before\n#[derive(Queryable)]\nstruct User<'a> {\n    name: &'a str,\n}\n\n// after\nuse std::borrow::Cow;\n#[derive(Queryable)]\nstruct User<'a> {\n    name: Cow<'a, str>,\n}\n// or simply: struct User { name: String }","handlingStrategy":"type-guard","validationCode":"// Compile-time check: this fails to compile if any field is a reference\nfn assert_no_refs<T>() {}\nstruct User { name: String }\nfn main() { assert_no_refs::<User>(); }","typeGuard":"fn has_reference_fields<'a>(s: &'a str) -> bool { std::mem::size_of_val(&s) == std::mem::size_of::<&'a str>() && false } // Prefer: simply use owned or Cow<'a, T> field types so the derive always compiles","tryCatchPattern":null,"preventionTips":["Use owned types (String, Vec<u8>) or Cow<'a, T> in any struct deriving Queryable/Selectable.","Never copy struct shapes with reference fields into diesel derives.","Run cargo check early; this is a compile-time error, so CI catches it immediately."],"tags":["rust","proc-macro","diesel","compile-time"],"backgroundTag":"unsupported-operation","analyzedSha":"6fa6ed01b24b24248ab2a611698d0a7c6a2e9120","analyzedAt":"2026-09-07T01:50:13.074Z","contentChangedAt":"2026-09-07T01:50:13.074Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}