{"record":{"id":"04a7c7eea7d7689b","repo":"siyuan-note/siyuan","slug":"sort-target-document-s-is-not-a-sibling-of-the","errorCode":null,"errorMessage":"sort target document [%s] is not a sibling of the new document","messagePattern":"sort target document \\[(.+?)\\] is not a sibling of the new document","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/file.go","lineNumber":1295,"sourceCode":"var createDocLock = sync.Mutex{}\n\nfunc CreateDocByMd(boxID, p, title, md string, sorts []string, arg map[string]any) (tree *parse.Tree, err error) {\n\tcreateDocLock.Lock()\n\tdefer createDocLock.Unlock()\n\n\tbox, err := getOpenedBox(boxID)\n\tif nil != err {\n\t\treturn\n\t}\n\tsortTargetID, _ := arg[\"sortTargetID\"].(string)\n\tsortPosition, _ := arg[\"sortPosition\"].(string)\n\tif \"\" != sortTargetID {\n\t\tif \"before\" != sortPosition && \"after\" != sortPosition {\n\t\t\treturn nil, fmt.Errorf(\"invalid sort position [%s]\", sortPosition)\n\t\t}\n\t\ttarget := treenode.GetBlockTree(sortTargetID)\n\t\tif !isSortableDocument(target) || target.BoxID != boxID || path.Dir(target.Path) != path.Dir(p) {\n\t\t\treturn nil, fmt.Errorf(\"sort target document [%s] is not a sibling of the new document\", sortTargetID)\n\t\t}\n\t} else if \"\" != sortPosition {\n\t\treturn nil, errors.New(\"sort target ID is required when sort position is specified\")\n\t}\n\n\tluteEngine := util.NewLute()\n\tluteEngine.SetHTMLTag2TextMark(true)\n\tdom := luteEngine.Md2BlockDOM(md, false)\n\ttree, err = createDoc(box.ID, p, title, dom, false)\n\tif err != nil {\n\t\treturn\n\t}\n\n\tFlushTxQueue()\n\tif \"\" != sortTargetID {\n\t\tif _, sortErr := ReorderDocs([]string{tree.ID}, sortTargetID, sortPosition); nil != sortErr {\n\t\t\tlogging.LogErrorf(\"reorder created document [%s] failed: %s\", tree.ID, sortErr)\n\t\t\tbox.setSortByConf(path.Dir(tree.Path), tree.ID)","sourceCodeStart":1277,"sourceCodeEnd":1313,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/8641553a1f07374001902d3ce773285db1292b2d/kernel/model/file.go#L1277-L1313","documentation":"When a sortTargetID is given, CreateDocByMd verifies the target is a sortable document, belongs to the same notebook (target.BoxID == boxID), and sits in the same parent directory as the new document's path (path.Dir equality). Failing any check returns fmt.Errorf(\"sort target document [%s] is not a sibling of the new document\").","triggerScenarios":"Calling CreateDocByMd with sortTargetID pointing to a block/document that is missing from the block tree (isSortableDocument false), lives in another notebook, or resides under a different parent path than the new document.","commonSituations":"Stale target ID after the target document was moved or deleted; cross-notebook drag operations passing a target from another box; passing a non-document block ID (e.g. a paragraph) as sortTargetID.","solutions":["Ensure sortTargetID refers to an existing document in the same notebook and same parent folder as the new document's path.","Re-fetch the target document's current ID/path after any move/rename before creating relative to it.","Only pass document-level block IDs (retrieved from the doc tree), never arbitrary block IDs.","Fall back to creating without sortTargetID when sibling placement cannot be guaranteed."],"exampleFix":"// before\ncreateDocWithMd({notebook: boxA, path, markdown, sortTargetID: docFromBoxB});\n// after\nif (target.boxID === boxA && path.dirname(target.path) === path.dirname(newPath)) {\n  createDocWithMd({notebook: boxA, path, markdown, sortTargetID: target.id});\n}","handlingStrategy":"validation","validationCode":"const target = await getBlockTree(sortTargetID);\nconst ok = target && target.isDocument && target.boxID === notebook &&\n  dirname(target.path) === dirname(newDocPath);\nif (!ok) { delete payload.sortTargetID; delete payload.sortPosition; }","typeGuard":"function isSortableSiblingTarget(t, boxId, newPath) {\n  return Boolean(t) && t.isDocument === true && t.boxID === boxId &&\n    dirname(t.path) === dirname(newPath);\n}","tryCatchPattern":"try {\n  await createDocWithMd(payload);\n} catch (e) {\n  if (String(e.msg).includes('not a sibling')) {\n    delete payload.sortTargetID;\n    await createDocWithMd(payload); // create without relative placement\n  }\n}","preventionTips":["Re-resolve target document IDs right before creating relative to them","Only pass document IDs from the same notebook and parent folder","Fall back to non-positional creation when the target cannot be verified"],"tags":["validation","document-creation","sort","sibling-check"],"backgroundTag":"invalid-argument-value","analyzedSha":"8641553a1f07374001902d3ce773285db1292b2d","analyzedAt":"2026-09-11T16:08:28.414Z","contentChangedAt":"2026-09-11T16:08:28.414Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}