siyuan-note/siyuan · error

template document tree parent snapshot is invalid

Error message

template document tree parent snapshot is invalid

What it means

After loading, the template parent snapshot must be non-nil. LoadTreeByBlockID can return (nil, nil) when no tree is found without an error; this guard converts that silent miss into an explicit error so the transaction does not proceed with a nil snapshot.

Solutions

  1. Confirm the template document exists and its root block ID is current
  2. Re-render the template from the live document to obtain a fresh root ID
  3. Rebuild the index if blocktree.db is stale
  4. Discard/reissue the stale transaction
Defensive patterns

Strategy: validation

Validate before calling

if tree, _ := LoadTreeByBlockID(rootID); tree == nil { return errors.New("template root no longer exists") }

Try / catch

if err := tx.Commit(); err != nil && strings.Contains(err.Error(), "template document tree parent snapshot is invalid") { re-renderTemplate(); retry() }

Prevention

When it happens

Trigger: Committing a template-render operation whose templateDocTreeRootID cannot be resolved to any document tree — the root block ID does not exist in the repository index.

Common situations: The template's parent document was deleted concurrently; the block ID belongs to a different notebook that is closed or unmounted; stale IDs left over after doc re-creation (deleted and recreated docs get new IDs).

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/0d5bd12105af4ede. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/transaction.go:2700

			if existingBox, exists := tx.restoredCreatedDocBoxes[operation.ID]; exists {
				if existingBox != operation.Tree.Box {
					tx.restoredCreatedDocBoxes[operation.ID] = ""
				}
			} else {
				tx.restoredCreatedDocBoxes[operation.ID] = operation.Tree.Box
			}
		}
		if ("restoreCreatedDoc" != operation.Action && "removeCreatedDoc" != operation.Action) ||
			"" == operation.templateDocTreeRootID || nil == operation.Tree {
			continue
		}
		if nil == tx.templateDocTreeRootSnapshot {
			snapshot, loadErr := LoadTreeByBlockID(operation.templateDocTreeRootID)
			if nil != loadErr {
				return fmt.Errorf("load template document tree parent snapshot failed: %w", loadErr)
			}
			if nil == snapshot {
				return errors.New("template document tree parent snapshot is invalid")
			}
			if nil != expectedTemplateDocTreeRoot &&
				(expectedTemplateDocTreeRoot.ID != snapshot.ID || expectedTemplateDocTreeRoot.Box != snapshot.Box ||
					expectedTemplateDocTreeRoot.Path != snapshot.Path || expectedTemplateDocTreeRoot.HPath != snapshot.HPath) {
				return errors.New("the document used to render the template has changed")
			}
			tx.templateDocTreeRootSnapshot = snapshot
		}
		if nil == tx.templateDocTreeRootSnapshot || tx.templateDocTreeRootSnapshot.ID != operation.templateDocTreeRootID ||
			tx.templateDocTreeRootSnapshot.Box != operation.Tree.Box {
			return errors.New("template document tree parent snapshot is invalid")
		}
	}
	tx.listItemFoldCandidates = nil
	tx.listItemFoldCandidateIDs = map[string]struct{}{}
	tx.deletedAttrViewBlockIDs = map[string]map[string]struct{}{}
	tx.deletedAttrViewCarriers = map[string]string{}
	tx.attributeViewDeletionUndo, tx.attributeViewDeletionErr = nil, nil

View on GitHub (pinned to 9f775e8a12)