From a8d3af2b62e7ba24f500624fd158069de981d08c Mon Sep 17 00:00:00 2001 From: hyungi Date: Mon, 29 Jun 2026 23:34:18 +0000 Subject: [PATCH] =?UTF-8?q?fix(clause-kb):=20backlinks=20=EC=97=94?= =?UTF-8?q?=EB=93=9C=ED=8F=AC=EC=9D=B8=ED=8A=B8=20parent=5Fid=20ORM=20?= =?UTF-8?q?=EB=AF=B8=EB=A7=A4=ED=95=91=20=E2=86=92=20raw=20SQL=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20(500=20=EC=88=98=EC=A0=95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.8 (1M context) --- app/api/documents.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/api/documents.py b/app/api/documents.py index 94f2090..f390738 100644 --- a/app/api/documents.py +++ b/app/api/documents.py @@ -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],