Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: The save path is incorrect when creating a sub-doc by ref in a doc with the same name https://github.com/siyuan-note/siyuan/issues/8138

Daniel5ad9de95415c
diff --git a/kernel/model/path.go b/kernel/model/path.go
index efd518f282..7979ce4b48 100644
--- a/kernel/model/path.go
+++ b/kernel/model/path.go
@@ -45,8 +45,8 @@ func createDocsByHPath(boxID, hPath, content, parentID, id string) (retID string
// 在指定了父文档 ID 的情况下优先查找父文档
parentHPath, name := path.Split(hPath)
parentHPath = strings.TrimSuffix(parentHPath, "/")
- preferredParent := treenode.GetBlockTreeRootByHPathPreferredParentID(boxID, parentHPath, parentID)
- if nil != preferredParent && preferredParent.ID == parentID {
+ preferredParent := treenode.GetBlockTreeByHPathPreferredParentID(boxID, parentHPath, parentID)
+ if nil != preferredParent && preferredParent.RootID == parentID {
// 如果父文档存在且 ID 一致,则直接在父文档下创建
p := strings.TrimSuffix(preferredParent.Path, ".sy") + "/" + id + ".sy"
if _, err = createDoc(boxID, p, name, content); err != nil {
diff --git a/kernel/treenode/blocktree.go b/kernel/treenode/blocktree.go
index 0030d0fbbb..0f535a96b2 100644
--- a/kernel/treenode/blocktree.go
+++ b/kernel/treenode/blocktree.go
@@ -258,10 +258,10 @@ func GetBlockTreeRootsByHPath(boxID, hPath string) (ret []*BlockTree) {
return
}
-func GetBlockTreeRootByHPathPreferredParentID(boxID, hPath, preferredParentID string) (ret *BlockTree) {
+func GetBlockTreeByHPathPreferredParentID(boxID, hPath, preferredParentID string) (ret *BlockTree) {
hPath = gulu.Str.RemoveInvisible(hPath)
var roots []*BlockTree
- sqlStmt := "SELECT * FROM blocktrees WHERE box_id = ? AND hpath = ? AND parent_id = ? AND type = 'd'"
+ sqlStmt := "SELECT * FROM blocktrees WHERE box_id = ? AND hpath = ? AND parent_id = ? LIMIT 1"
rows, err := db.Query(sqlStmt, boxID, hPath, preferredParentID)
if err != nil {
logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)