From 9b1eff0c0db78c335fc1ea13c7909a2df200ec12 Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Sun, 12 May 2024 09:22:38 +0200 Subject: [PATCH] Switch relationship loading to selectionload (#758) --- sqladmin/_queries.py | 4 ++-- sqladmin/models.py | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/sqladmin/_queries.py b/sqladmin/_queries.py index 3e3e1112..54e88acc 100644 --- a/sqladmin/_queries.py +++ b/sqladmin/_queries.py @@ -3,7 +3,7 @@ import anyio from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession -from sqlalchemy.orm import Session, joinedload +from sqlalchemy.orm import Session, selectinload from sqlalchemy.sql.expression import Select, and_, or_ from starlette.requests import Request @@ -152,7 +152,7 @@ async def _update_async( stmt = self.model_view._stmt_by_identifier(pk) for relation in self.model_view._form_relations: - stmt = stmt.options(joinedload(relation)) + stmt = stmt.options(selectinload(relation)) async with self.model_view.session_maker(expire_on_commit=False) as session: result = await session.execute(stmt) diff --git a/sqladmin/models.py b/sqladmin/models.py index f075ecdc..b31163ba 100644 --- a/sqladmin/models.py +++ b/sqladmin/models.py @@ -20,7 +20,7 @@ import anyio from sqlalchemy import Column, String, asc, cast, desc, func, inspect, or_ from sqlalchemy.exc import NoInspectionAvailable -from sqlalchemy.orm import joinedload, sessionmaker +from sqlalchemy.orm import selectinload, sessionmaker from sqlalchemy.orm.exc import DetachedInstanceError from sqlalchemy.sql.elements import ClauseElement from sqlalchemy.sql.expression import Select, select @@ -772,7 +772,7 @@ async def list(self, request: Request) -> Pagination: stmt = self.list_query(request) for relation in self._list_relations: - stmt = stmt.options(joinedload(relation)) + stmt = stmt.options(selectinload(relation)) stmt = self.sort_query(stmt, request) @@ -802,7 +802,7 @@ async def get_model_objects( stmt = self.list_query(request).limit(limit) for relation in self._list_relations: - stmt = stmt.options(joinedload(relation)) + stmt = stmt.options(selectinload(relation)) rows = await self._run_query(stmt) return rows @@ -815,7 +815,7 @@ async def get_object_for_details(self, value: Any) -> Any: stmt = self._stmt_by_identifier(value) for relation in self._details_relations: - stmt = stmt.options(joinedload(relation)) + stmt = stmt.options(selectinload(relation)) return await self._get_object_by_pk(stmt) @@ -1062,7 +1062,7 @@ def edit_form_query(self, request: Request) -> Select: stmt = self._stmt_by_identifier(request.path_params["pk"]) for relation in self._form_relations: - stmt = stmt.options(joinedload(relation)) + stmt = stmt.options(selectinload(relation)) return stmt def count_query(self, request: Request) -> Select: