More patching.
This commit is contained in:
parent
ffa49f13e9
commit
7db182041e
1 changed files with 17 additions and 5 deletions
|
|
@ -539,7 +539,7 @@ class CRUDService(Generic[T]):
|
|||
limit, offset = spec.parse_pagination()
|
||||
|
||||
# Includes / fields (populates join_paths)
|
||||
root_fields, rel_field_names, root_field_names = spec.parse_fields()
|
||||
root_fields, rel_field_names, root_field_names, collection_field_names = spec.parse_fields()
|
||||
spec.parse_includes()
|
||||
join_paths = tuple(spec.get_join_paths())
|
||||
|
||||
|
|
@ -548,12 +548,24 @@ class CRUDService(Generic[T]):
|
|||
if only_cols:
|
||||
query = query.options(Load(root_alias).load_only(*only_cols))
|
||||
|
||||
# JOIN all paths we resolved and hydrate them from the join
|
||||
# JOIN non-collection paths; selectinload for collections
|
||||
used_contains_eager = False
|
||||
for _base_alias, rel_attr, target_alias in join_paths:
|
||||
query = query.join(target_alias, rel_attr.of_type(target_alias), isouter=True)
|
||||
query = query.options(contains_eager(rel_attr, alias=target_alias))
|
||||
used_contains_eager = True
|
||||
is_collection = bool(getattr(getattr(rel_attr, "property", None), "uselist", False))
|
||||
if is_collection:
|
||||
opt = selectinload(rel_attr)
|
||||
child_names = (collection_field_names or {}).get(rel_attr.key, [])
|
||||
if child_names:
|
||||
target_cls = rel_attr.property.mapper.class_
|
||||
cols = [getattr(target_cls, n, None) for n in child_names]
|
||||
cols = [c for c in cols if isinstance(c, InstrumentedAttribute)]
|
||||
if cols:
|
||||
opt = opt.load_only(*cols)
|
||||
query = query.options(opt)
|
||||
else:
|
||||
query = query.join(target_alias, rel_attr.of_type(target_alias), isouter=True)
|
||||
query = query.options(contains_eager(rel_attr, alias=target_alias))
|
||||
used_contains_eager = True
|
||||
|
||||
# Filters AFTER joins → no cartesian products
|
||||
if filters:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue