diff --git a/backend/routes/booking.py b/backend/routes/booking.py index a17df64..c4a7528 100644 --- a/backend/routes/booking.py +++ b/backend/routes/booking.py @@ -5,6 +5,7 @@ from fastapi import APIRouter, Depends, HTTPException, Query from sqlalchemy import select, and_, func from sqlalchemy.exc import IntegrityError from sqlalchemy.ext.asyncio import AsyncSession +from sqlalchemy.orm import joinedload from auth import require_permission from database import get_db @@ -254,9 +255,11 @@ async def cancel_my_appointment( current_user: User = Depends(require_permission(Permission.CANCEL_OWN_APPOINTMENT)), ) -> AppointmentResponse: """Cancel one of the current user's appointments.""" - # Get the appointment + # Get the appointment with explicit eager loading of user relationship result = await db.execute( - select(Appointment).where(Appointment.id == appointment_id) + select(Appointment) + .options(joinedload(Appointment.user)) + .where(Appointment.id == appointment_id) ) appointment = result.scalar_one_or_none() @@ -311,10 +314,11 @@ async def get_all_appointments( total = count_result.scalar() or 0 total_pages = (total + per_page - 1) // per_page if total > 0 else 1 - # Get paginated appointments (user relationship is eager-loaded via lazy="joined") + # Get paginated appointments with explicit eager loading of user relationship offset = (page - 1) * per_page result = await db.execute( select(Appointment) + .options(joinedload(Appointment.user)) .order_by(Appointment.slot_start.desc()) .offset(offset) .limit(per_page) @@ -343,9 +347,11 @@ async def admin_cancel_appointment( _current_user: User = Depends(require_permission(Permission.CANCEL_ANY_APPOINTMENT)), ) -> AppointmentResponse: """Cancel any appointment (admin only).""" - # Get the appointment + # Get the appointment with explicit eager loading of user relationship result = await db.execute( - select(Appointment).where(Appointment.id == appointment_id) + select(Appointment) + .options(joinedload(Appointment.user)) + .where(Appointment.id == appointment_id) ) appointment = result.scalar_one_or_none()