Skip to content

Commit 69868b4

Browse files
committed
refactor: improve table typing
1 parent 3c4c93f commit 69868b4

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

  • diracx-db/src/diracx/db/sql/utils

diracx-db/src/diracx/db/sql/utils/base.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
from uuid import UUID as StdUUID # noqa: N811
1313

1414
from pydantic import TypeAdapter
15-
from sqlalchemy import DateTime, MetaData, func, select
15+
from sqlalchemy import DateTime, MetaData, func, inspect, select
1616
from sqlalchemy.exc import OperationalError
1717
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncEngine, create_async_engine
18+
from sqlalchemy.orm import DeclarativeBase
1819
from uuid_utils import UUID, uuid7
1920

2021
from diracx.core.exceptions import InvalidQueryError
@@ -250,7 +251,7 @@ async def ping(self):
250251

251252
async def _search(
252253
self,
253-
table: Any,
254+
table: type[DeclarativeBase],
254255
parameters: list[str] | None,
255256
search: list[SearchSpec],
256257
sorts: list[SortSpec],
@@ -290,12 +291,15 @@ async def _search(
290291
]
291292

292293
async def _summary(
293-
self, table: Any, group_by: list[str], search: list[SearchSpec]
294+
self,
295+
table: type[DeclarativeBase],
296+
group_by: list[str],
297+
search: list[SearchSpec],
294298
) -> list[dict[str, str | int]]:
295299
"""Get a summary of the elements of a table."""
296300
columns = _get_columns(table.__table__, group_by)
297301

298-
pk_columns = list(table.__table__.primary_key.columns)
302+
pk_columns = list(inspect(table).primary_key)
299303
if not pk_columns:
300304
raise ValueError(
301305
"Model has no primary key and no count_column was provided."

0 commit comments

Comments
 (0)