fix(clause-kb): backlinks 엔드포인트 parent_id ORM 미매핑 → raw SQL 조회 (500 수정)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
hyungi
2026-06-29 23:34:18 +00:00
parent 51a7c96b56
commit a8d3af2b62
+11 -5
View File
@@ -1904,6 +1904,12 @@ async def get_document_backlinks(
if not doc or doc.deleted_at is not None:
raise HTTPException(status_code=404, detail="문서를 찾을 수 없습니다")
_meta = (await session.execute(sql_text(
"SELECT parent_id, clause_code, clause_order FROM documents WHERE id = :id"
).bindparams(id=doc_id))).mappings().first()
_parent_id = _meta["parent_id"] if _meta else None
_clause_code = _meta["clause_code"] if _meta else None
_clause_order = _meta["clause_order"] if _meta else None
forward = (
await session.execute(
sql_text(
@@ -1934,7 +1940,7 @@ async def get_document_backlinks(
).mappings().all()
prev = nxt = None
if doc.parent_id is not None and doc.clause_order is not None:
if _parent_id is not None and _clause_order is not None:
prow = (
await session.execute(
sql_text(
@@ -1944,7 +1950,7 @@ async def get_document_backlinks(
AND clause_order < :ord
ORDER BY clause_order DESC LIMIT 1
"""
).bindparams(pid=doc.parent_id, ord=doc.clause_order)
).bindparams(pid=_parent_id, ord=_clause_order)
)
).mappings().first()
nrow = (
@@ -1956,7 +1962,7 @@ async def get_document_backlinks(
AND clause_order > :ord
ORDER BY clause_order ASC LIMIT 1
"""
).bindparams(pid=doc.parent_id, ord=doc.clause_order)
).bindparams(pid=_parent_id, ord=_clause_order)
)
).mappings().first()
prev = ClauseTocItem(**dict(prow)) if prow else None
@@ -1964,8 +1970,8 @@ async def get_document_backlinks(
return BacklinksResponse(
doc_id=doc_id,
clause_code=doc.clause_code,
parent_id=doc.parent_id,
clause_code=_clause_code,
parent_id=_parent_id,
prev=prev,
next=nxt,
forward=[BacklinkRef(**dict(r)) for r in forward],