From 14054bfd6d768900303d86df5c5054d90cdef63c Mon Sep 17 00:00:00 2001 From: Samir Boulahtit Date: Mon, 1 Dec 2025 22:10:08 +0100 Subject: [PATCH] fix: add unique() to company query with joinedload SQLAlchemy Error Fix: - Add .unique() when using joinedload(Company.vendors) - When eagerly loading collection relationships with joinedload, SQLAlchemy can return duplicate rows - The unique() method deduplicates results and is required for joined collection loads Error was: InvalidRequestError: The unique() method must be invoked on this Result, as it contains results that include joined eager loads against collections This is a standard SQLAlchemy pattern for handling one-to-many relationships with eager loading. --- app/services/company_service.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/services/company_service.py b/app/services/company_service.py index 17503608..badf015b 100644 --- a/app/services/company_service.py +++ b/app/services/company_service.py @@ -157,7 +157,8 @@ class CompanyService: # Apply pagination and order query = query.order_by(Company.name).offset(skip).limit(limit) - companies = list(db.execute(query).scalars().all()) + # Use unique() when using joinedload with collections to avoid duplicate rows + companies = list(db.execute(query).scalars().unique().all()) return companies, total