{"record":{"id":"142997b6b4548817","repo":"siyuan-note/siyuan","slug":"cannot-change-field-type-to-primary-key","errorCode":null,"errorMessage":"cannot change field type to primary key","messagePattern":"cannot change field type to primary key","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/attribute_view.go","lineNumber":7710,"sourceCode":"\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}\n\t}\n","sourceCodeStart":7692,"sourceCodeEnd":7728,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/attribute_view.go#L7692-L7728","documentation":"Companion to the primary-key protection: the kernel forbids changing any existing non-block column INTO the block (primary key) type. Only one primary key exists per attribute view, and it is created with the view; promoting another column to block type is rejected to keep row identity unambiguous.","triggerScenarios":"Calling the update-column type-change API with operation.Typ = 'block' targeting a key whose current type is not block.","commonSituations":"Plugin UI that offers 'block' in a generic type dropdown; scripts converting CSV-like data where one column is intended as the row identifier; bulk type normalization code.","solutions":["Do not offer/select 'block' as a change target for existing keys; it is only set at view creation","Remove operation.ID/type-change operations that set Typ to block","Create a new view or use the existing block key instead of promoting a column","Adjust the plugin to disable the block option in the type menu"],"exampleFix":"// before\nop.Typ = \"block\" // trying to promote column to primary key\n// after\nop.Typ = \"text\" // use an existing key type; the block key already exists in the view","handlingStrategy":"validation","validationCode":"if (op.Typ === \"block\") throw new Error(\"cannot promote an existing key to block; the primary key already exists\");","typeGuard":"const isPromotionToBlock = (targetType) => targetType === \"block\";","tryCatchPattern":"try { await updateColType(op); } catch (e) { if (String(e).includes(\"cannot change field type to primary key\")) { /* remove the type change or target a different type */ } else { throw e; } }","preventionTips":["Never list 'block' as a re-type target in UI dropdowns for existing keys","Understand the block key is created with the view and is unique","Review type-change payloads for accidental 'block' values in bulk scripts"],"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"}