Schemas
class Parent:
relationship(ChildA) #One-to-Many
relationship(ChildB, lazy="joined") #One-to-Many
relationship(ChildC, lazy="joined") #One-to-Many
class ChildA:
parent_id
array_of_enums
id
class ChildB:
parent_id
class ChildC:
parent_id
Goal
Query for Parent
, ChildA
pairs in which ChildA.array_of_enums
contains a subset of enum values.
Query
session.query(Parent, ChildA.array_of_enums).filter(
Parent.attr == specified_value,
Parent.id == ChildA.parent_id,
ChildA.array_of_enums.contains(enums)
ChildA.attr == specified_value_2
).all()
Question
SQLAlchemy attaches joinedload options for ChildB
& ChildC
and results in returning (Parent, ChildA.array_of_enums) for each child of ChildB
& Child C
. As a result, I'm getting too many of Parent
, ChildA
pairs (extra for each of children B & children C).
Is there a way to build the query, such that all children B and children C come in with Parent all together without a separate SQL statement?
Also interestingly, querying for ChildA.id
(or any other column) as opposed to ChildA.array_of_enums
does not result in "duplicated" results.