diff --git a/backend/src/api/routes/documents.py b/backend/src/api/routes/documents.py index 5ac9386..f193a5e 100644 --- a/backend/src/api/routes/documents.py +++ b/backend/src/api/routes/documents.py @@ -506,25 +506,55 @@ async def delete_document( if document.thumbnail_path and os.path.exists(document.thumbnail_path): os.remove(document.thumbnail_path) - # 관련 데이터 먼저 삭제 (외래키 제약 조건 해결) + # 관련 데이터 안전하게 삭제 (외래키 제약 조건 해결) from ...models.highlight import Highlight from ...models.note import Note from ...models.bookmark import Bookmark - # 메모 먼저 삭제 (하이라이트를 참조하므로) - await db.execute(delete(Note).where(Note.document_id == document_id)) - - # 북마크 삭제 - await db.execute(delete(Bookmark).where(Bookmark.document_id == document_id)) - - # 마지막으로 하이라이트 삭제 - await db.execute(delete(Highlight).where(Highlight.document_id == document_id)) - - # 문서-태그 관계 삭제 (Document.tags 관계를 통해 자동 처리됨) - - # 마지막으로 문서 삭제 - await db.execute(delete(Document).where(Document.id == document_id)) - await db.commit() + try: + print(f"DEBUG: Starting deletion of document {document_id}") + + # 1. 먼저 해당 문서의 모든 하이라이트 ID 조회 + highlight_ids_result = await db.execute(select(Highlight.id).where(Highlight.document_id == document_id)) + highlight_ids = [row[0] for row in highlight_ids_result.fetchall()] + print(f"DEBUG: Found {len(highlight_ids)} highlights to delete") + + # 2. 하이라이트에 연결된 모든 메모 삭제 + total_notes_deleted = 0 + for highlight_id in highlight_ids: + note_result = await db.execute(delete(Note).where(Note.highlight_id == highlight_id)) + total_notes_deleted += note_result.rowcount + print(f"DEBUG: Deleted {total_notes_deleted} notes by highlight_id") + + # 3. document_id로 직접 연결된 메모도 삭제 (혹시 있다면) + direct_note_result = await db.execute(delete(Note).where(Note.document_id == document_id)) + print(f"DEBUG: Deleted {direct_note_result.rowcount} notes by document_id") + + # 4. 북마크 삭제 + bookmark_result = await db.execute(delete(Bookmark).where(Bookmark.document_id == document_id)) + print(f"DEBUG: Deleted {bookmark_result.rowcount} bookmarks") + + # 5. 하이라이트 삭제 (이제 메모가 모두 삭제되었으므로 안전) + highlight_result = await db.execute(delete(Highlight).where(Highlight.document_id == document_id)) + print(f"DEBUG: Deleted {highlight_result.rowcount} highlights") + + # 6. 문서-태그 관계는 SQLAlchemy가 자동으로 처리 + + # 7. 마지막으로 문서 삭제 + doc_result = await db.execute(delete(Document).where(Document.id == document_id)) + print(f"DEBUG: Deleted {doc_result.rowcount} documents") + + # 8. 커밋 + await db.commit() + print(f"DEBUG: Successfully deleted document {document_id}") + + except Exception as e: + print(f"ERROR: Failed to delete document {document_id}: {e}") + await db.rollback() + raise HTTPException( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + detail=f"Failed to delete document: {str(e)}" + ) return {"message": "Document deleted successfully"} diff --git a/backend/src/api/routes/highlights.py b/backend/src/api/routes/highlights.py index 68d4ca0..4386983 100644 --- a/backend/src/api/routes/highlights.py +++ b/backend/src/api/routes/highlights.py @@ -381,9 +381,30 @@ async def delete_highlight( detail="Not enough permissions" ) - # 하이라이트 삭제 (CASCADE로 메모도 함께 삭제됨) - await db.execute(delete(Highlight).where(Highlight.id == highlight_id)) - await db.commit() + # 안전한 하이라이트 삭제 (연결된 메모 먼저 삭제) + try: + print(f"DEBUG: Starting deletion of highlight {highlight_id}") + + # 1. 먼저 연결된 메모 삭제 + from ...models.note import Note + note_result = await db.execute(delete(Note).where(Note.highlight_id == highlight_id)) + print(f"DEBUG: Deleted {note_result.rowcount} notes for highlight {highlight_id}") + + # 2. 하이라이트 삭제 + highlight_result = await db.execute(delete(Highlight).where(Highlight.id == highlight_id)) + print(f"DEBUG: Deleted {highlight_result.rowcount} highlights") + + # 3. 커밋 + await db.commit() + print(f"DEBUG: Successfully deleted highlight {highlight_id}") + + except Exception as e: + print(f"ERROR: Failed to delete highlight {highlight_id}: {e}") + await db.rollback() + raise HTTPException( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + detail=f"Failed to delete highlight: {str(e)}" + ) return {"message": "Highlight deleted successfully"} diff --git a/frontend/hierarchy.html b/frontend/hierarchy.html index af7c5b4..4b738c7 100644 --- a/frontend/hierarchy.html +++ b/frontend/hierarchy.html @@ -144,7 +144,7 @@
- +