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
This commit is contained in:
counterweight 2025-12-21 17:57:23 +01:00
parent 64eeaadd28
commit c24597edb4
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C

View file

@ -5,6 +5,7 @@ from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy import select, and_, func from sqlalchemy import select, and_, func
from sqlalchemy.exc import IntegrityError from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import joinedload
from auth import require_permission from auth import require_permission
from database import get_db from database import get_db
@ -254,9 +255,11 @@ async def cancel_my_appointment(
current_user: User = Depends(require_permission(Permission.CANCEL_OWN_APPOINTMENT)), current_user: User = Depends(require_permission(Permission.CANCEL_OWN_APPOINTMENT)),
) -> AppointmentResponse: ) -> AppointmentResponse:
"""Cancel one of the current user's appointments.""" """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( 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() appointment = result.scalar_one_or_none()
@ -311,10 +314,11 @@ async def get_all_appointments(
total = count_result.scalar() or 0 total = count_result.scalar() or 0
total_pages = (total + per_page - 1) // per_page if total > 0 else 1 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 offset = (page - 1) * per_page
result = await db.execute( result = await db.execute(
select(Appointment) select(Appointment)
.options(joinedload(Appointment.user))
.order_by(Appointment.slot_start.desc()) .order_by(Appointment.slot_start.desc())
.offset(offset) .offset(offset)
.limit(per_page) .limit(per_page)
@ -343,9 +347,11 @@ async def admin_cancel_appointment(
_current_user: User = Depends(require_permission(Permission.CANCEL_ANY_APPOINTMENT)), _current_user: User = Depends(require_permission(Permission.CANCEL_ANY_APPOINTMENT)),
) -> AppointmentResponse: ) -> AppointmentResponse:
"""Cancel any appointment (admin only).""" """Cancel any appointment (admin only)."""
# Get the appointment # Get the appointment with explicit eager loading of user relationship
result = await db.execute( 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() appointment = result.scalar_one_or_none()