fix: topological insert for KB import nodes to satisfy parent FK
Nodes with parent_node_id references were inserted in a single batch, causing FK violations when children were inserted before their parents. Now inserts roots first, flushes, then children in subsequent passes. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -498,9 +498,25 @@ async def convert_document(
|
|||||||
await db.flush()
|
await db.flush()
|
||||||
return []
|
return []
|
||||||
|
|
||||||
# Persist nodes
|
# Persist nodes — insert roots first to satisfy parent_node_id FK,
|
||||||
for node in nodes:
|
# then children in subsequent passes until all are inserted.
|
||||||
db.add(node)
|
remaining = list(nodes)
|
||||||
|
inserted_ids: set[Any] = set()
|
||||||
|
while remaining:
|
||||||
|
batch = [
|
||||||
|
n for n in remaining
|
||||||
|
if n.parent_node_id is None or n.parent_node_id in inserted_ids
|
||||||
|
]
|
||||||
|
if not batch:
|
||||||
|
# Circular reference or orphan — force insert remaining to surface the real error
|
||||||
|
for n in remaining:
|
||||||
|
db.add(n)
|
||||||
|
break
|
||||||
|
for n in batch:
|
||||||
|
db.add(n)
|
||||||
|
inserted_ids.add(n.id)
|
||||||
|
await db.flush()
|
||||||
|
remaining = [n for n in remaining if n.id not in inserted_ids]
|
||||||
|
|
||||||
# Update import record
|
# Update import record
|
||||||
elapsed_ms = int((time.monotonic() - start_time) * 1000)
|
elapsed_ms = int((time.monotonic() - start_time) * 1000)
|
||||||
|
|||||||
Reference in New Issue
Block a user