{"record":{"id":"b49864551321b3d5","repo":"diesel-rs/diesel","slug":"deriving-aschangeset-on-a-structure-that-only-co","errorCode":null,"errorMessage":"deriving `AsChangeset` on a structure that only contains primary keys isn't supported.\nhelp: if you want to change the primary key of a row, you should do so with `.set(table::id.eq(new_id))`.\nnote: `#[derive(AsChangeset)]` never changes the primary key of a row.","messagePattern":"deriving `AsChangeset` on a structure that only contains primary keys isn't supported\\.\nhelp: if you want to change the primary key of a row, you should do so with `\\.set\\(table::id\\.eq\\(new_id\\)\\)`\\.\nnote: `#\\[derive\\(AsChangeset\\)\\]` never changes the primary key of a row\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"diesel_derives/src/as_changeset.rs","lineNumber":31,"sourceCode":"pub fn derive(item: DeriveInput) -> Result<TokenStream> {\n    let model = Model::from_item(&item, false, false)?;\n\n    let struct_name = &item.ident;\n    let table_name = &model.table_names()[0];\n\n    let fields_for_update = model\n        .fields()\n        .iter()\n        .filter(|f| {\n            !model\n                .primary_key_names\n                .iter()\n                .any(|p| f.column_name().map(|f| f == *p).unwrap_or_default())\n        })\n        .collect::<Vec<_>>();\n\n    if fields_for_update.is_empty() {\n        return Err(syn::Error::new(\n            proc_macro2::Span::mixed_site(),\n            \"deriving `AsChangeset` on a structure that only contains primary keys isn't supported.\\n\\\n             help: if you want to change the primary key of a row, you should do so with `.set(table::id.eq(new_id))`.\\n\\\n             note: `#[derive(AsChangeset)]` never changes the primary key of a row.\",\n        ));\n    }\n\n    let treat_none_as_null = model.treat_none_as_null();\n\n    let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl();\n\n    let mut generate_borrowed_changeset = true;\n\n    let mut direct_field_ty = Vec::with_capacity(fields_for_update.len());\n    let mut direct_field_assign = Vec::with_capacity(fields_for_update.len());\n    let mut ref_field_ty = Vec::with_capacity(fields_for_update.len());\n    let mut ref_field_assign = Vec::with_capacity(fields_for_update.len());\n","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/diesel-rs/diesel/blob/6fa6ed01b24b24248ab2a611698d0a7c6a2e9120/diesel_derives/src/as_changeset.rs#L13-L49","documentation":"`#[derive(AsChangeset)]` builds the set of columns to update from the struct's non-primary-key fields. If after removing primary-key fields nothing remains, the derive cannot generate a meaningful changeset and refuses to compile, explaining that primary keys are never changed by this derive.","triggerScenarios":"Deriving `AsChangeset` on a struct where every field is annotated as (or resolves to) the table's primary key, so `fields_for_update` is empty.","commonSituations":"A struct representing just a row's ID(s) (e.g. `struct UserId { id: i32 }`); structs whose only column is the PK after renames via `#[column_name]`.","solutions":["Add at least one non-primary-key field to the struct","Remove the `AsChangeset` derive and use `.set(table::id.eq(new_id))` directly to change the primary key","Use `dsl::UpdateTarget`/`update(...).set(...)` manually instead of the derive"],"exampleFix":"// before\n#[derive(AsChangeset)]\nstruct UserPk { id: i32 }\n// after — change PK directly\n diesel::update(users.find(id)).set(users.id.eq(new_id))","handlingStrategy":"validation","validationCode":"// Before deriving, ensure at least one field is not a primary key\nlet non_pk = fields.iter().filter(|f| !is_primary_key(f)).count();\nassert!(non_pk > 0, \"AsChangeset requires at least one non-PK field\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only derive AsChangeset on structs with updatable columns","Change primary keys with `.set(table::id.eq(new_id))` instead","Don't put AsChangeset on ID-only wrapper structs"],"tags":["rust","diesel","proc-macro","as-changeset"],"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"}