{"record":{"id":"0b65500fa512be34","repo":"siyuan-note/siyuan","slug":"previous-key-not-found-in-current-view-s","errorCode":null,"errorMessage":"previous key not found in current view: %s","messagePattern":"previous key not found in current view: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/mcp/tools/database.go","lineNumber":356,"sourceCode":"\tview, err := attrView.GetFirstView()\n\tif nil != err {\n\t\treturn \"\", err\n\t}\n\tfieldIDs := databaseViewFieldIDs(view)\n\tif value, specified := args[\"prev\"]; specified {\n\t\tprev, ok := value.(string)\n\t\tif !ok {\n\t\t\treturn \"\", errors.New(\"prev must be a string\")\n\t\t}\n\t\tif \"\" == prev {\n\t\t\treturn \"\", nil\n\t\t}\n\t\tfor _, fieldID := range fieldIDs {\n\t\t\tif fieldID == prev {\n\t\t\t\treturn prev, nil\n\t\t\t}\n\t\t}\n\t\treturn \"\", fmt.Errorf(\"previous key not found in current view: %s\", prev)\n\t}\n\tif 0 < len(fieldIDs) {\n\t\treturn fieldIDs[len(fieldIDs)-1], nil\n\t}\n\treturn \"\", nil\n}\n\nfunc databaseKeySetTemplate(args map[string]any) (CallToolResult, error) {\n\tid, _ := args[\"id\"].(string)\n\tkeyID, _ := args[\"keyID\"].(string)\n\ttemplateContent, ok := args[\"template\"].(string)\n\tif \"\" == id || \"\" == keyID || !ok {\n\t\treturn CallToolResult{Content: []ContentItem{{Type: \"text\", Text: \"id, keyID and template (string, may be empty) are required\"}}, IsError: true}, nil\n\t}\n\tif err := model.SetAttributeViewKeyTemplate(id, keyID, templateContent); nil != err {\n\t\treturn CallToolResult{Content: []ContentItem{{Type: \"text\", Text: \"set key template failed: \" + err.Error()}}, IsError: true}, nil\n\t}\n\tmodel.ReloadAttrView(id)","sourceCodeStart":338,"sourceCodeEnd":374,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/mcp/tools/database.go#L338-L374","documentation":"After type-checking, databasePreviousKeyID verifies the supplied `prev` ID actually exists among the current view's field IDs; an unknown ID is rejected with this formatted error naming the ID. This prevents inserting a key at a dangling position.","triggerScenarios":"databaseKeyAdd called with prev set to a field ID that does not belong to the current view — deleted key, wrong database, stale ID after key reordering/removal, or ID from another view.","commonSituations":"Caching field IDs across edits that later removed the key; copy-pasting IDs between databases; LLM hallucinating plausible-looking IDs.","solutions":["Fetch the current view's field IDs first and pick prev from that list","Omit prev to default to appending after the view's last field","Verify the ID belongs to the same database/view you are modifying","Handle the formatted error by re-reading the view and retrying with a valid ID"],"exampleFix":"// before\nawait call(\"database_key_add\", {prev: staleId})\n// after\nconst fields = await getDatabaseViewFieldIDs(avId)\nawait call(\"database_key_add\", {prev: fields[fields.length - 1]})","handlingStrategy":"validation","validationCode":"const fields = await getViewFieldIDs(view); if (prev && !fields.includes(prev)) throw new Error(`unknown prev: ${prev}`);","typeGuard":null,"tryCatchPattern":"try { await call(\"database_key_add\", {prev}) } catch (e) { if (String(e).startsWith(\"previous key not found\")) { prev = undefined; return call(\"database_key_add\", {prev}); } throw e; }","preventionTips":["Re-read field IDs before each key-add; never cache stale IDs","Omit prev to append at view end","Verify IDs come from the same database"],"tags":["mcp","database","not-found","stale-reference"],"backgroundTag":"entity-not-found","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"}