diff --git a/backend/app/api/v1/exam.py b/backend/app/api/v1/exam.py index dd17673..442b6d3 100644 --- a/backend/app/api/v1/exam.py +++ b/backend/app/api/v1/exam.py @@ -66,7 +66,7 @@ async def start_exam( user_id = current_user.id username = current_user.username - exam = await ExamService.start_exam( + exam_id = await ExamService.start_exam( db=db, user_id=user_id, course_id=request.course_id, @@ -96,7 +96,7 @@ async def start_exam( ) ) - return ResponseModel(code=200, data=StartExamResponse(exam_id=exam.id), message="考试开始") + return ResponseModel(code=200, data=StartExamResponse(exam_id=exam_id), message="考试开始") @router.post("/submit", response_model=ResponseModel[SubmitExamResponse]) diff --git a/backend/app/services/exam_service.py b/backend/app/services/exam_service.py index e682cf8..53a783e 100644 --- a/backend/app/services/exam_service.py +++ b/backend/app/services/exam_service.py @@ -20,7 +20,7 @@ class ExamService: @staticmethod async def start_exam( db: AsyncSession, user_id: int, course_id: int, question_count: int = 10 - ) -> Exam: + ) -> int: """ 开始考试 @@ -31,7 +31,7 @@ class ExamService: question_count: 题目数量 Returns: - Exam: 考试实例 + int: 考试ID """ # 检查课程是否存在 course = await db.get(Course, course_id) @@ -93,8 +93,10 @@ class ExamService: db.add(exam) await db.commit() await db.refresh(exam) - - return exam + + # 返回exam.id而不是整个对象,避免懒加载问题 + exam_id = exam.id + return exam_id @staticmethod async def submit_exam(