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 3
3
from sqlalchemy .orm import DeclarativeBase , Mapped , mapped_column
4
4
5
5
6
- class Base (DeclarativeBase ):
6
+ class BaseWithoutId (DeclarativeBase ):
7
7
__table_args__ : ClassVar [dict [str , Any ]] = {
8
8
"extend_existing" : True ,
9
9
}
10
10
11
+
12
+ class Base (BaseWithoutId ):
13
+ __abstract__ = True
14
+
11
15
id : Mapped [int ] = mapped_column (primary_key = True )
Original file line number Diff line number Diff line change 11
11
from fastapi_jsonapi .data_layers .sqla .query_building import RelationshipInfo
12
12
from fastapi_jsonapi .data_typing import TypeModel
13
13
from fastapi_jsonapi .exceptions import BadRequest , InternalServerError , ObjectNotFound
14
+ from fastapi_jsonapi .storages .models_storage import models_storage
14
15
15
16
log = logging .getLogger (__name__ )
16
17
@@ -97,8 +98,10 @@ async def count(
97
98
cls ,
98
99
session : AsyncSession ,
99
100
stmt : Select ,
101
+ resource_type : str ,
100
102
) -> 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 ())
102
105
return (await session .execute (stmt )).scalar_one ()
103
106
104
107
@classmethod
Original file line number Diff line number Diff line change @@ -428,6 +428,7 @@ async def get_collection(
428
428
objects_count = await self ._base_sql .count (
429
429
session = self .session ,
430
430
stmt = query ,
431
+ resource_type = self .resource_type ,
431
432
)
432
433
433
434
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(
264
264
attrs_schema = schemas_storage .get_attrs_schema (resource_type , operation_type = "get" )
265
265
266
266
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
+ )
268
271
data_schema = schemas_storage .get_data_schema (resource_type , operation_type = "get" )
269
272
return data_schema (
270
- id = f"{ db_item . id } " ,
273
+ id = f"{ id_val } " ,
271
274
attributes = attrs_schema .model_validate (db_item ),
272
275
).model_dump ()
273
276
You can’t perform that action at this time.
0 commit comments