D112: 修复ensure_path递归创建父节点·解决FOREIGN KEY约束

This commit is contained in:
铸渊 ICE-GL-ZY001 2026-05-25 01:57:07 +00:00
parent 4f610d3596
commit 5a6b8f216d

View File

@ -114,10 +114,23 @@ class HLDPTreeStore:
def ensure_path(self, path: str, node_type: str, persona_id: str = None, def ensure_path(self, path: str, node_type: str, persona_id: str = None,
epoch_id: str = None, title: str = "", summary: str = "", epoch_id: str = None, title: str = "", summary: str = "",
parent_path: str = None) -> str: parent_path: str = None) -> str:
"""确保树路径存在,不存在则创建。返回 path。""" """确保树路径存在,不存在则创建(含递归父节点)。返回 path。"""
# 先确保父路径存在
if parent_path:
parent_exists = self.conn.execute(
"SELECT 1 FROM hldp_nodes WHERE path=?", (parent_path,)).fetchone()
if not parent_exists:
# 递归创建父节点
grandparent = "/".join(parent_path.split("/")[:-1]) if "/" in parent_path else None
pp_type = "epoch" if parent_path.count("/") == 2 else \
"index" if parent_path.count("/") == 3 else "branch"
self.ensure_path(
path=parent_path, node_type=pp_type,
persona_id=persona_id, epoch_id=epoch_id,
parent_path=grandparent)
cur = self.conn.execute("SELECT path FROM hldp_nodes WHERE path=?", (path,)) cur = self.conn.execute("SELECT path FROM hldp_nodes WHERE path=?", (path,))
if cur.fetchone(): if cur.fetchone():
# 更新
self.conn.execute( self.conn.execute(
"UPDATE hldp_nodes SET title=?, summary=?, updated_at=datetime('now') WHERE path=?", "UPDATE hldp_nodes SET title=?, summary=?, updated_at=datetime('now') WHERE path=?",
(title, summary, path)) (title, summary, path))