{"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":7708,"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":7690,"sourceCodeEnd":7726,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/attribute_view.go#L7690-L7726","documentation":"In the attribute view column-type-change path, the kernel forbids changing the type of the primary key field (the block-type key) away from block. The primary key identifies rows as blocks, so re-typing it would break row identity. When the existing key is the primary key and the requested type is not block, this error is returned.","triggerScenarios":"Calling the update-column / set-column-type API targeting the block-type key's ID with operation.Typ set to any non-block type such as 'text' or 'number'.","commonSituations":"Plugin or script iterating all keys trying to normalize types including the block key; user-facing code that lets the primary key be selected for re-typing; bulk migration scripts over .av files.","solutions":["Skip the primary (block-type) key when batch-changing column types","Detect the primary key by checking keyValues.Key.Type == av.KeyTypeBlock and exclude it from type-change operations","Change the target operation.ID to a non-primary key","If the semantic goal is a different column, add a new key of the desired type instead of re-typing the block key"],"exampleFix":"// before\nfor _, key := range attrView.KeyValues { changeType(key.Key.ID, newType) }\n// after\nfor _, key := range attrView.KeyValues {\n    if key.Key.Type == av.KeyTypeBlock { continue } // never re-type the primary key\n    changeType(key.Key.ID, newType)\n}","handlingStrategy":"validation","validationCode":"const key = attrView.KeyValues.find(kv => kv.Key.ID === op.ID);\nif (key && key.Key.Type === \"block\" && op.Typ !== \"block\") throw new Error(\"primary key type is immutable\");","typeGuard":"const isPrimaryKey = (key) => key.type === \"block\";","tryCatchPattern":"try { await updateColType(op); } catch (e) { if (String(e).includes(\"cannot change type of primary key field\")) { /* skip or create a new key instead */ } else { throw e; } }","preventionTips":["Treat block-type keys as immutable in type-change code paths","Filter out the primary key when offering re-typing options in UI","Add new keys for new types rather than mutating the block key"],"tags":["attribute-view","primary-key","unsupported"],"backgroundTag":"unsupported-operation","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}