From edc17a7b3d7149e6800b5603e1bc2e3e9355944d Mon Sep 17 00:00:00 2001 From: Katy Baulch <46493669+katybaulch@users.noreply.github.com> Date: Thu, 19 Dec 2024 14:52:25 +0000 Subject: [PATCH] Added lazy load to prevent multiplicity --- app/main.py | 2 +- app/repository/family.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 46bd5f1d..34011678 100644 --- a/app/main.py +++ b/app/main.py @@ -134,5 +134,5 @@ async def root(): app, host="0.0.0.0", port=8888, - log_config=DEFAULT_LOGGING, + # log_config=DEFAULT_LOGGING, ) # type: ignore diff --git a/app/repository/family.py b/app/repository/family.py index 2e11a80b..e6562ae0 100644 --- a/app/repository/family.py +++ b/app/repository/family.py @@ -25,7 +25,7 @@ from sqlalchemy import desc, func, or_ from sqlalchemy import update as db_update from sqlalchemy.exc import NoResultFound, OperationalError -from sqlalchemy.orm import Query, Session +from sqlalchemy.orm import Query, Session, lazyload from sqlalchemy_utils import escape_like from app.errors import RepositoryError @@ -66,7 +66,7 @@ def _get_query(db: Session) -> Query: .subquery() ) - return ( + query = ( db.query( Family, geography_subquery.c.geography_ids, @@ -92,7 +92,15 @@ def _get_query(db: Session) -> Query: Corpus.import_id, Organisation, ) + .options( + # Disable any default eager loading as this was causing multiplicity due to + # implicit joins in relationships on the selected models. + lazyload("*") + ) ) + _LOGGER.error(query) + + return query def _family_to_dto(