{"record":{"id":"e5e576fa5c7104cd","repo":"siyuan-note/siyuan","slug":"query-embed-block-statement-not-found","errorCode":null,"errorMessage":"query embed block statement not found","messagePattern":"query embed block statement not found","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/search.go","lineNumber":305,"sourceCode":"\t}\n\tif treenode.TypeAbbr(ast.NodeBlockQueryEmbed.String()) != bt.Type {\n\t\terr = errors.New(\"not query embed block\")\n\t\treturn\n\t}\n\n\ttree, loadErr := filesys.LoadTree(bt.BoxID, bt.Path, util.NewLute())\n\tif nil != loadErr {\n\t\terr = loadErr\n\t\treturn\n\t}\n\tnode := treenode.GetNodeInTree(tree, embedBlockID)\n\tif nil == node || ast.NodeBlockQueryEmbed != node.Type {\n\t\terr = ErrBlockNotFound\n\t\treturn\n\t}\n\tscriptNode := node.ChildByType(ast.NodeBlockQueryEmbedScript)\n\tif nil == scriptNode {\n\t\terr = errors.New(\"query embed block statement not found\")\n\t\treturn\n\t}\n\n\tstmt = stdhtml.UnescapeString(scriptNode.TokensStr())\n\tstmt = strings.ReplaceAll(stmt, editor.IALValEscNewLine, \"\\n\")\n\tboxID = bt.BoxID\n\treturn\n}\n\nfunc SearchEmbedBlock(embedBlockID, stmt string, excludeIDs []string, headingMode int, breadcrumb bool) (ret []*EmbedBlock) {\n\treturn SearchEmbedBlockInBox(embedBlockID, stmt, excludeIDs, headingMode, breadcrumb, \"\")\n}\n\n// SearchEmbedBlockInBox 与 SearchEmbedBlock 一致，但按 boxID 路由 SQL 到加密 content db。\n// 加密笔记本的嵌入块查询走独立加密库（全局 siyuan.db 不含加密数据），boxID 为空时落回全局库。\nfunc SearchEmbedBlockInBox(embedBlockID, stmt string, excludeIDs []string, headingMode int, breadcrumb bool, boxID string) (ret []*EmbedBlock) {\n\treturn searchEmbedBlockInBox(embedBlockID, stmt, excludeIDs, headingMode, breadcrumb, boxID, true)\n}","sourceCodeStart":287,"sourceCodeEnd":323,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/kernel/model/search.go#L287-L323","documentation":"The GetQueryEmbedStatement function found a valid NodeBlockQueryEmbed block and loaded its tree, but the block has no child node of type NodeBlockQueryEmbedScript. This means the embed block exists but its SQL query statement content is missing, corrupted, or was never set. The script node is where the actual query text (e.g., 'SELECT * FROM blocks WHERE...') is stored in the AST.","triggerScenarios":"Calling GetQueryEmbedStatement on an embed block whose AST node has no NodeBlockQueryEmbedScript child. This can happen with malformed or partially constructed embed blocks, blocks whose script content was deleted or emptied, or data corruption in the .sy file where the script child node is missing from the serialized tree.","commonSituations":"An embed block was created programmatically or via direct .sy file editing without including the script content. Also occurs when a data sync conflict partially merges an embed block, keeping the container node but dropping the script child. Rare but possible after a Lute version upgrade that changes the expected child node structure of embed blocks.","solutions":["Inspect the .sy file for the embed block to verify whether the script child node exists in the serialized AST","If the script is genuinely missing, recreate the embed block through the editor UI to regenerate the proper node structure","If this occurs after a sync, check for sync conflicts in the .sy file and resolve them to restore the complete embed block AST","As a fallback, the caller can catch this error and prompt the user to re-enter the embed query"],"exampleFix":"// before\nstmt, boxID, err := model.GetQueryEmbedStatement(embedBlockID)\nif err != nil {\n    log.Fatal(err)\n}\n\n// after\nstmt, boxID, err := model.GetQueryEmbedStatement(embedBlockID)\nif err != nil && err.Error() == \"query embed block statement not found\" {\n    // prompt user to recreate the embed block query\n    stmt = \"\"\n    err = nil\n}","handlingStrategy":"validation","validationCode":"// Before extracting embed statement, verify script node exists\n// This requires loading the tree and checking the AST node structure\nbt := treenode.GetBlockTree(embedBlockID)\nif bt != nil {\n    tree, _ := filesys.LoadTree(bt.BoxID, bt.Path, util.NewLute())\n    if tree != nil {\n        node := treenode.GetNodeInTree(tree, embedBlockID)\n        if node != nil && node.ChildByType(ast.NodeBlockQueryEmbedScript) == nil {\n            // script node missing — block is malformed\n            return \"\", \"\", nil\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"// Handle missing embed script gracefully\nstmt, boxID, err := model.GetQueryEmbedStatement(embedBlockID)\nif err != nil && strings.Contains(err.Error(), \"statement not found\") {\n    // embed block has no query — treat as empty\n    stmt = \"\"\n    err = nil\n}","preventionTips":["When creating embed blocks programmatically, always include the NodeBlockQueryEmbedScript child node","Validate embed block AST integrity after sync operations that may partially merge .sy files","If this error recurs for the same block, inspect the .sy file directly to diagnose structural corruption","Provide a repair utility that scans for embed blocks missing their script child and recreates them"],"tags":["embed-block","ast","data-corruption","missing-content"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}