File tree Expand file tree Collapse file tree 4 files changed +15
-4
lines changed
examples/api_for_sqlalchemy/models Expand file tree Collapse file tree 4 files changed +15
-4
lines changed Original file line number Diff line number Diff line change 33from sqlalchemy .orm import DeclarativeBase , Mapped , mapped_column
44
55
6- class Base (DeclarativeBase ):
6+ class BaseWithoutId (DeclarativeBase ):
77 __table_args__ : ClassVar [dict [str , Any ]] = {
88 "extend_existing" : True ,
99 }
1010
11+
12+ class Base (BaseWithoutId ):
13+ __abstract__ = True
14+
1115 id : Mapped [int ] = mapped_column (primary_key = True )
Original file line number Diff line number Diff line change 1111from fastapi_jsonapi .data_layers .sqla .query_building import RelationshipInfo
1212from fastapi_jsonapi .data_typing import TypeModel
1313from fastapi_jsonapi .exceptions import BadRequest , InternalServerError , ObjectNotFound
14+ from fastapi_jsonapi .storages .models_storage import models_storage
1415
1516log = logging .getLogger (__name__ )
1617
@@ -97,8 +98,10 @@ async def count(
9798 cls ,
9899 session : AsyncSession ,
99100 stmt : Select ,
101+ resource_type : str ,
100102 ) -> int :
101- stmt = select (func .count (distinct (column ("id" )))).select_from (stmt .subquery ())
103+ id_col = models_storage .get_model_id_field_name (resource_type )
104+ stmt = select (func .count (distinct (column (id_col )))).select_from (stmt .subquery ())
102105 return (await session .execute (stmt )).scalar_one ()
103106
104107 @classmethod
Original file line number Diff line number Diff line change @@ -428,6 +428,7 @@ async def get_collection(
428428 objects_count = await self ._base_sql .count (
429429 session = self .session ,
430430 stmt = query ,
431+ resource_type = self .resource_type ,
431432 )
432433
433434 collection = await self .after_get_collection (collection , qs , view_kwargs )
Original file line number Diff line number Diff line change @@ -264,10 +264,13 @@ def _prepare_item_data(
264264 attrs_schema = schemas_storage .get_attrs_schema (resource_type , operation_type = "get" )
265265
266266 if include_fields is None or not (field_schemas := include_fields .get (resource_type )):
267-
267+ id_val = models_storage .get_object_id (
268+ db_object = db_item ,
269+ resource_type = resource_type ,
270+ )
268271 data_schema = schemas_storage .get_data_schema (resource_type , operation_type = "get" )
269272 return data_schema (
270- id = f"{ db_item . id } " ,
273+ id = f"{ id_val } " ,
271274 attributes = attrs_schema .model_validate (db_item ),
272275 ).model_dump ()
273276
You can’t perform that action at this time.
0 commit comments