{"record":{"id":"5ef7822273ecd89b","repo":"diesel-rs/diesel","slug":"embed-and-treat-none-as-default-value-are-mutu-5ef782","errorCode":null,"errorMessage":"`embed` and `treat_none_as_default_value` are mutually exclusive","messagePattern":"`embed` and `treat_none_as_default_value` are mutually exclusive","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"diesel_derives/src/insertable.rs","lineNumber":61,"sourceCode":"    let mut ref_field_ty = Vec::with_capacity(model.fields().len());\n    let mut ref_field_assign = Vec::with_capacity(model.fields().len());\n\n    // Explicit trait bounds to improve error messages\n    let mut field_ty_bounds = Vec::with_capacity(model.fields().len());\n    let mut borrowed_field_ty_bounds = Vec::with_capacity(model.fields().len());\n    let mut field_ty_bounds_guard = HashMap::new();\n    let mut borrowed_field_ty_bounds_guard = HashMap::new();\n\n    for field in model.fields() {\n        // skip this field while generating the insertion\n        if field.skip_insertion() {\n            continue;\n        }\n        // Use field-level attr. with fallback to the struct-level one.\n        let treat_none_as_default_value = match &field.treat_none_as_default_value {\n            Some(attr) => {\n                if let Some(embed) = &field.embed {\n                    return Err(syn::Error::new(\n                        embed.attribute_span,\n                        \"`embed` and `treat_none_as_default_value` are mutually exclusive\",\n                    ));\n                }\n\n                if !is_option_ty(&field.ty) {\n                    return Err(syn::Error::new(\n                        field.ty.span(),\n                        \"expected `treat_none_as_default_value` field to be of type `Option<_>`\",\n                    ));\n                }\n\n                attr.item\n            }\n            None => treat_none_as_default_value,\n        };\n\n        match (field.serialize_as.as_ref(), field.embed()) {","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/diesel-rs/diesel/blob/6fa6ed01b24b24248ab2a611698d0a7c6a2e9120/diesel_derives/src/insertable.rs#L43-L79","documentation":"In the `Insertable` derive, a field marked `#[diesel(embed)]` (splicing another table's columns in) cannot also set `treat_none_as_default_value`, since embedded fields don't map to a single column with a default. The check in derive_into_single_table rejects the combination at insertable.rs:61.","triggerScenarios":"A struct-level or field-level `treat_none_as_default_value` attribute applied to a field that also has `#[diesel(embed)]`.","commonSituations":"Setting `treat_none_as_default_value` at the struct level while some fields embed nested insertables; copy-pasting option/default handling onto embedded fields.","solutions":["Remove `treat_none_as_default_value` from the embedded field (or from the struct level for that field).","Apply `treat_none_as_default_value` only to plain `Option<T>` fields, not embedded ones.","If you need default handling inside the embedded struct, set it within that nested type's own derive."],"exampleFix":"// before\n#[derive(Insertable)]\n#[diesel(table_name = posts)]\nstruct NewPost {\n  #[diesel(embed, treat_none_as_default_value = true)]\n  meta: PostMeta,\n}\n\n// after\n#[derive(Insertable)]\n#[diesel(table_name = posts)]\nstruct NewPost {\n  #[diesel(embed)]\n  meta: PostMeta,\n}","handlingStrategy":"validation","validationCode":"// Never combine embed with treat_none_as_default_value on one field:\n// #[diesel(embed)]                     // OK\n// #[diesel(embed, treat_none_as_default_value = true)] // FAILS","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Reserve `treat_none_as_default_value` for plain Option<T> fields.","Audit struct-level `treat_none_as_default_value` when adding embedded fields.","Configure defaults inside the embedded type's own derive instead."],"tags":["proc-macro","derive","insertable","attribute-conflict","compile-time"],"backgroundTag":"mutually-exclusive-options","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"}