{"record":{"id":"6bffef72ce95538e","repo":"siyuan-note/siyuan","slug":"load-template-document-tree-parent-snapshot-failed","errorCode":null,"errorMessage":"load template document tree parent snapshot failed: %w","messagePattern":"load template document tree parent snapshot failed: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/transaction.go","lineNumber":2697,"sourceCode":"\t\tif \"restoreCreatedDoc\" == operation.Action && \"\" != operation.templateDocTreeRootID &&\n\t\t\tnil != operation.Tree && nil != operation.Tree.Root &&\n\t\t\toperation.ID == operation.Tree.Root.ID {\n\t\t\tif existingBox, exists := tx.restoredCreatedDocBoxes[operation.ID]; exists {\n\t\t\t\tif existingBox != operation.Tree.Box {\n\t\t\t\t\ttx.restoredCreatedDocBoxes[operation.ID] = \"\"\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\ttx.restoredCreatedDocBoxes[operation.ID] = operation.Tree.Box\n\t\t\t}\n\t\t}\n\t\tif (\"restoreCreatedDoc\" != operation.Action && \"removeCreatedDoc\" != operation.Action) ||\n\t\t\t\"\" == operation.templateDocTreeRootID || nil == operation.Tree {\n\t\t\tcontinue\n\t\t}\n\t\tif nil == tx.templateDocTreeRootSnapshot {\n\t\t\tsnapshot, loadErr := LoadTreeByBlockID(operation.templateDocTreeRootID)\n\t\t\tif nil != loadErr {\n\t\t\t\treturn fmt.Errorf(\"load template document tree parent snapshot failed: %w\", loadErr)\n\t\t\t}\n\t\t\tif nil == snapshot {\n\t\t\t\treturn errors.New(\"template document tree parent snapshot is invalid\")\n\t\t\t}\n\t\t\tif nil != expectedTemplateDocTreeRoot &&\n\t\t\t\t(expectedTemplateDocTreeRoot.ID != snapshot.ID || expectedTemplateDocTreeRoot.Box != snapshot.Box ||\n\t\t\t\t\texpectedTemplateDocTreeRoot.Path != snapshot.Path || expectedTemplateDocTreeRoot.HPath != snapshot.HPath) {\n\t\t\t\treturn errors.New(\"the document used to render the template has changed\")\n\t\t\t}\n\t\t\ttx.templateDocTreeRootSnapshot = snapshot\n\t\t}\n\t\tif nil == tx.templateDocTreeRootSnapshot || tx.templateDocTreeRootSnapshot.ID != operation.templateDocTreeRootID ||\n\t\t\ttx.templateDocTreeRootSnapshot.Box != operation.Tree.Box {\n\t\t\treturn errors.New(\"template document tree parent snapshot is invalid\")\n\t\t}\n\t}\n\ttx.listItemFoldCandidates = nil\n\ttx.listItemFoldCandidateIDs = map[string]struct{}{}","sourceCodeStart":2679,"sourceCodeEnd":2715,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/transaction.go#L2679-L2715","documentation":"During transaction commit, an operation that renders a template document requires the parent document tree snapshot to be loaded via LoadTreeByBlockID. If that load fails (indexing error, missing database entry, corrupted tree), the transaction aborts with this wrapped error preserving the underlying cause.","triggerScenarios":"Committing a transaction whose operation carries templateDocTreeRootID but LoadTreeByBlockID returns an error — the template's root block ID is no longer in blocktree/SQL index, or the underlying storage read failed.","commonSituations":"The template document was deleted between rendering and commit; the SQL index is out of sync or being rebuilt; a corrupted .sy file removed the root block; concurrent transaction deleted the parent doc.","solutions":["Read the wrapped cause (%w) to identify the load failure","Verify the template document still exists (check data/<box>/.sy files)","Rebuild the index (Settings - Rebuild index) to restore LoadTreeByBlockID coverage","Retry the transaction after the document tree is restored"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"snapshot, err := LoadTreeByBlockID(templateDocTreeRootID)\nif err != nil || snapshot == nil { /* abort before building the tx operation */ }","typeGuard":null,"tryCatchPattern":"if err := tx.Commit(); err != nil {\n    var wrapped error\n    if errors.As(err, &wrapped) && strings.Contains(err.Error(), \"load template document tree parent snapshot failed\") {\n        // rebuild index / verify doc exists, then retry\n    }\n}","preventionTips":["Do not delete the template's parent document while template operations are pending","Keep the SQL index healthy; rebuild after crashes","Unwrap the %w cause to diagnose the underlying load failure","Retry the transaction after the document tree is restored"],"tags":["transaction","template","indexing"],"backgroundTag":"resource-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"}