From c24597edb474137703d4eda2fb7c016a19973213 Mon Sep 17 00:00:00 2001 From: counterweight Date: Sun, 21 Dec 2025 17:57:23 +0100 Subject: [PATCH] Be explicit about eager loading in queries - Added explicit joinedload(Appointment.user) to admin appointment queries - Makes the eager loading intention clear and explicit - Replaced comment-based documentation with actual query options --- backend/routes/booking.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) 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()