{"record":{"id":"7295d3e4899c528a","repo":"siyuan-note/siyuan","slug":"cannot-change-type-of-primary-key-field","errorCode":null,"errorMessage":"cannot change type of primary key field","messagePattern":"cannot change type of primary key field","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/attribute_view.go","lineNumber":7171,"sourceCode":"\nfunc updateAttributeViewColumn(operation *Operation) (err error) {\n\tattrView, err := av.ParseAttributeView(operation.AvID)\n\tif err != nil {\n\t\treturn\n\t}\n\n\tcolType := av.KeyType(operation.Typ)\n\tchangeType := false\n\tswitch colType {\n\tcase av.KeyTypeBlock, av.KeyTypeText, av.KeyTypeNumber, av.KeyTypeDate, av.KeyTypeSelect, av.KeyTypeMSelect, av.KeyTypeURL, av.KeyTypeEmail,\n\t\tav.KeyTypePhone, av.KeyTypeMAsset, av.KeyTypeTemplate, av.KeyTypeCreated, av.KeyTypeUpdated, av.KeyTypeCheckbox,\n\t\tav.KeyTypeRelation, av.KeyTypeRollup, av.KeyTypeLineNumber:\n\t\tfor _, keyValues := range attrView.KeyValues {\n\t\t\tif keyValues.Key.ID == operation.ID {\n\t\t\t\tisPrimaryKey := av.KeyTypeBlock == keyValues.Key.Type\n\t\t\t\tif isPrimaryKey != (av.KeyTypeBlock == colType) {\n\t\t\t\t\tif isPrimaryKey {\n\t\t\t\t\t\terr = errors.New(\"cannot change type of primary key field\")\n\t\t\t\t\t} else {\n\t\t\t\t\t\terr = errors.New(\"cannot change field type to primary key\")\n\t\t\t\t\t}\n\t\t\t\t\treturn\n\t\t\t\t}\n\n\t\t\t\tkeyValues.Key.Name = strings.TrimSpace(operation.Name)\n\n\t\t\t\tchangeType = keyValues.Key.Type != colType\n\t\t\t\tkeyValues.Key.Type = colType\n\n\t\t\t\tfor _, value := range keyValues.Values {\n\t\t\t\t\tvalue.Type = colType\n\t\t\t\t}\n\n\t\t\t\tbreak\n\t\t\t}\n\t\t}","sourceCodeStart":7153,"sourceCodeEnd":7189,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/kernel/model/attribute_view.go#L7153-L7189","documentation":"Thrown by updateAttributeViewColumn when changing the type of the primary key (KeyTypeBlock) to any other type. The block key is the structural primary column and must remain type 'block'; its identity cannot be converted to text/number/etc. The inverse direction (changing another column into a block key) produces the sibling error 'cannot change field type to primary key'.","triggerScenarios":"A UI or script issues an updateAttrViewColumn operation whose ID targets the primary key but whose Typ is not 'block'. This can happen if the frontend mistakenly offers a type-change affordance on the primary column, or if a replayed operation set references the block key ID.","commonSituations":"A bulk 'change all columns to X' script that does not skip the primary key; a plugin that lets users retype every column; copy of operations from one AV applied to another where the IDs collide with the primary key.","solutions":["Skip the primary (block) key when offering or applying type-change operations on the client.","Detect KeyTypeBlock on the target column before sending updateAttrViewColumn and disable the type selector for it.","When scripting bulk retypes, filter out the AV's block key ID explicitly."],"exampleFix":"// before\nupdateAttributeViewColumn({ avID, id: primaryKeyID, typ: 'text', name })\n// after\nconst target = attrView.GetKey(primaryKeyID)\nif (target.Type === 'block') {\n  throw new Error('primary key type cannot be changed')\n}\nupdateAttributeViewColumn({ avID, id: primaryKeyID, typ: 'text', name })","handlingStrategy":"validation","validationCode":"const target = (await getAttrViewKeys(avID)).find(k => k.id === op.id)\nif (target && target.type === 'block') { /* disable type-change UI for primary key; skip op */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Skip the primary (block) key when offering type-change actions.","Filter block-key IDs out of any bulk retype script."],"tags":["attribute-view","primary-key","update-column","type-change"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}