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.
This commit is contained in:
2025-12-01 22:10:08 +01:00
parent 999427f37d
commit 14054bfd6d

View File

@@ -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