Eagerly load Image.client relationship in queries
CI / Lint & Format (push) Failing after 11s
CI / Tests (push) Has been skipped
CI / Security Scan (push) Failing after 8s
CI / Docker Build (push) Has been skipped

This commit is contained in:
2026-06-24 21:32:55 -04:00
parent a415526c66
commit 342cb30657
+6 -5
View File
@@ -12,6 +12,7 @@ from fastapi import (
)
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, func, or_
from sqlalchemy.orm import selectinload
from app.database import get_db
from app.dependencies.auth import get_current_client, require_scope
@@ -47,7 +48,7 @@ async def get_image_or_404(
Les autres clients sont limités à leurs propres images.
Lève HTTP 404 si introuvable ou si elle n'appartient pas au client.
"""
query = select(Image).where(Image.id == image_id)
query = select(Image).options(selectinload(Image.client)).where(Image.id == image_id)
if not client.has_scope("admin"):
query = query.where(Image.client_id == client.id)
result = await db.execute(query)
@@ -165,12 +166,12 @@ async def list_images(
# Admin : voit toutes les images, ou filtrées par client_id
if client.has_scope("admin"):
if client_id:
query = select(Image).where(Image.client_id == client_id)
query = select(Image).options(selectinload(Image.client)).where(Image.client_id == client_id)
else:
query = select(Image)
query = select(Image).options(selectinload(Image.client))
else:
# Filtre d'isolation par client
query = select(Image).where(Image.client_id == client.id)
query = select(Image).options(selectinload(Image.client)).where(Image.client_id == client.id)
if status_filter:
query = query.where(Image.processing_status == status_filter)
@@ -223,7 +224,7 @@ async def list_images(
uploaded_at=img.uploaded_at,
processing_status=img.processing_status,
client_id=img.client_id,
client_name=client.name,
client_name=img.client.name if img.client else None,
ai_tags=img.ai_tags,
ai_description=img.ai_description,
thumbnail_path=img.thumbnail_path,