{"record":{"id":"678b954936563bed","repo":"siyuan-note/siyuan","slug":"attribute-view-has-no-primary-key-field","errorCode":null,"errorMessage":"attribute view has no primary key field","messagePattern":"attribute view has no primary key field","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/attribute_view_create.go","lineNumber":134,"sourceCode":"\tif bt := treenode.GetBlockTree(blockID); nil != bt {\n\t\tutil.PushReloadProtyle(bt.RootID)\n\t}\n\tReloadAttrView(avID)\n\tret = &AttributeViewCreateResult{BlockID: blockID, AvID: avID, ViewID: view.ID, AttributeView: attrView}\n\treturn\n}\n\nfunc configureCreatedAttributeView(attrView *av.AttributeView, name, primaryKeyName string, preparedKeys []*av.Key) (err error) {\n\tif nil == attrView {\n\t\treturn errors.New(\"attribute view is required\")\n\t}\n\tcurrentView, err := attrView.GetFirstView()\n\tif nil != err {\n\t\treturn\n\t}\n\tblockKeyValues := attrView.GetBlockKeyValues()\n\tif nil == blockKeyValues || nil == blockKeyValues.Key {\n\t\treturn errors.New(\"attribute view has no primary key field\")\n\t}\n\n\tif name = strings.TrimSpace(name); \"\" != name {\n\t\tname = strings.ReplaceAll(name, \"\\n\", \" \")\n\t\tif 512 < utf8.RuneCountInString(name) {\n\t\t\tname = string([]rune(name)[:512])\n\t\t}\n\t\tattrView.Name = name\n\t}\n\tif primaryKeyName = strings.TrimSpace(primaryKeyName); \"\" != primaryKeyName {\n\t\tblockKeyValues.Key.Name = primaryKeyName\n\t}\n\n\tretainedKeyValues := []*av.KeyValues{blockKeyValues}\n\tretainedKeyIDs := map[string]bool{blockKeyValues.Key.ID: true}\n\tvar kanbanGroupKey *av.Key\n\tif av.LayoutTypeKanban == currentView.LayoutType {\n\t\tfor _, key := range preparedKeys {","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/attribute_view_create.go#L116-L152","documentation":"After creating the attribute view, configureCreatedAttributeView fetches its block (primary) key values via GetBlockKeyValues and requires that the key exists. A nil key values or nil Key means the new database lacks its mandatory primary (block) key field, which SiYuan databases always have, so configuration aborts. This indicates the default primary key was not created during initialization.","triggerScenarios":"Calling configureCreatedAttributeView on an attribute view that was not initialized with a block key (e.g. hand-constructed av.AttributeView without keyValues); a failure in the creation path that silently skipped adding the block key.","commonSituations":"Tests or plugins building av.AttributeView structs manually and passing them to the helper; corruption or partial writes during database creation; filtering out the block key when constructing keys.","solutions":["Initialize the attribute view with a block key (av.NewKeyValues with the block key) before configuring","Use the normal CreateAttributeViewDatabase path, which creates the primary key automatically, instead of hand-building the view","Inspect the created keys and ensure the key whose type is block was not removed"],"exampleFix":"// before\nattrView := &av.AttributeView{ID: avID}\nerr = configureCreatedAttributeView(attrView, name, pk, keys)\n// after\nblockKeyValues := &av.KeyValues{Key: &av.Key{ID: ast.NewNodeID(), Name: av.NodeAttrNameAvs, Type: av.KeyTypeBlock}}\nattrView := av.NewAttributeView(avID)\nattrView.KeyValues = append(attrView.KeyValues, blockKeyValues)\nerr = configureCreatedAttributeView(attrView, name, pk, keys)","handlingStrategy":"validation","validationCode":"bkv := attrView.GetBlockKeyValues()\nif bkv == nil || bkv.Key == nil { return errors.New(\"attribute view lacks primary key; re-create it\") }","typeGuard":"func hasPrimaryKey(av *av.AttributeView) bool {\n    kv := av.GetBlockKeyValues()\n    return kv != nil && kv.Key != nil\n}","tryCatchPattern":"if err := configureCreatedAttributeView(attrView, ...); err != nil {\n    if strings.Contains(err.Error(), \"no primary key field\") { return recreateAttributeView(avID) }\n    return err\n}","preventionTips":["Always create databases through CreateAttributeViewDatabase so the block key is initialized","Do not strip block-typed keys when constructing KeyValues","Validate a freshly created AV with hasPrimaryKey before further configuration"],"tags":["attribute-view","primary-key","internal-invariant"],"backgroundTag":"internal-invariant-violation","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"}