diesel-rs/diesel · error · syn::Error
foreign_derive can only be used with structs
Error message
foreign_derive can only be used with structs
What it means
Compile-time error from ty_for_foreign_derive, used by derives on models marked #[foreign_derive]. The foreign-derive path only supports struct items (it takes the first field's type as the mapped type); when the annotated item is an enum or union, this guard fires. Fix: apply the derive to a struct, or drop #[foreign_derive].
Solutions
- Use a struct with the foreign type as a field
- Remove foreign_derive from non-struct items
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at diesel_derives/src/util.rs:71 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of diesel-rs/diesel@6fa6ed01b2 (2026-09-07).
Data as JSON: /api/errors/299e4cddf6dd5599.
Report an issue: GitHub.
Appendix: source
Thrown at diesel_derives/src/util.rs:71
}
_ => None,
}
}
_ => None,
}
}
pub fn ty_for_foreign_derive(item: &DeriveInput, model: &Model) -> Result<Type> {
if model.foreign_derive {
match item.data {
Data::Struct(ref body) => match body.fields.iter().next() {
Some(field) => Ok(field.ty.clone()),
None => Err(syn::Error::new(
proc_macro2::Span::mixed_site(),
"foreign_derive requires at least one field",
)),
},
_ => Err(syn::Error::new(
proc_macro2::Span::mixed_site(),
"foreign_derive can only be used with structs",
)),
}
} else {
let ident = &item.ident;
let (_, ty_generics, ..) = item.generics.split_for_impl();
Ok(parse_quote!(#ident #ty_generics))
}
}
pub fn camel_to_snake(name: &str) -> String {
let mut result = String::with_capacity(name.len());
result.push_str(&name[..1].to_lowercase());
for character in name[1..].chars() {
if character.is_uppercase() {
result.push('_');
for lowercase in character.to_lowercase() {View on GitHub (pinned to 6fa6ed01b2)