siyuan-note/siyuan · error
previous key not found in current view
Error message
previous key not found in current view: %s
What it means
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.
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
Example fix
// before
await call("database_key_add", {prev: staleId})
// after
const fields = await getDatabaseViewFieldIDs(avId)
await call("database_key_add", {prev: fields[fields.length - 1]}) Defensive patterns
Strategy: validation
Validate before calling
const fields = await getViewFieldIDs(view); if (prev && !fields.includes(prev)) throw new Error(`unknown prev: ${prev}`); Try / catch
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; } Prevention
- 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
When it happens
Trigger: 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.
Common situations: Caching field IDs across edits that later removed the key; copy-pasting IDs between databases; LLM hallucinating plausible-looking IDs.
Understand the failure class
Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.
Related errors
- attribute view not found
- Conf.Language(0)
- database not found
- each key must be an object
- each key requires name and type
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/0b65500fa512be34.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/mcp/tools/database.go:356
view, err := attrView.GetFirstView()
if nil != err {
return "", err
}
fieldIDs := databaseViewFieldIDs(view)
if value, specified := args["prev"]; specified {
prev, ok := value.(string)
if !ok {
return "", errors.New("prev must be a string")
}
if "" == prev {
return "", nil
}
for _, fieldID := range fieldIDs {
if fieldID == prev {
return prev, nil
}
}
return "", fmt.Errorf("previous key not found in current view: %s", prev)
}
if 0 < len(fieldIDs) {
return fieldIDs[len(fieldIDs)-1], nil
}
return "", nil
}
func databaseKeySetTemplate(args map[string]any) (CallToolResult, error) {
id, _ := args["id"].(string)
keyID, _ := args["keyID"].(string)
templateContent, ok := args["template"].(string)
if "" == id || "" == keyID || !ok {
return CallToolResult{Content: []ContentItem{{Type: "text", Text: "id, keyID and template (string, may be empty) are required"}}, IsError: true}, nil
}
if err := model.SetAttributeViewKeyTemplate(id, keyID, templateContent); nil != err {
return CallToolResult{Content: []ContentItem{{Type: "text", Text: "set key template failed: " + err.Error()}}, IsError: true}, nil
}
model.ReloadAttrView(id)View on GitHub (pinned to 9f775e8a12)