Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >>> sub = session.query(
- >>> ChildA.parent_id,
- >>> func.count(ChildA.id).label('count'),
- >>> )
- >>> sub = sub.group_by(ChildA.parent_id)
- >>> sub = query.subquery()
- >>>
- >>> # this works
- >>> query1 = session.query(
- >>> ChildB.someattr,
- >>> sub.c.count,
- >>> )
- >>> query1 = query1.select_from(ChildB)
- >>> print(query1.outerjoin((sub, sub.c.parent_id == ChildB.parent_id)))
- SELECT child_b.someattr AS child_b_someattr, anon1.count AS anon1_count
- FROM child_b LEFT OUTER JOIN (SELECT child_a.parent_id AS child_a_parent_id, COUNT(child_a.id) AS count
- FROM child_a GROUP BY child_a.parent_id) AS anon1 ON anon1.parent_id = child_b.parent_id
- >>>
- >>> # this doesn't
- >>> query2 = session.query(
- >>> sub.c.count,
- >>> ChildB.someattr,
- >>> )
- >>> query2 = query2.select_from(sub)
- >>> print(query2.outerjoin((ChildB, sub.c.parent_id == ChildB.parent_id)))
- Traceback (most recent call last):
- ...
- InvalidRequestError: Could not find a FROM clause to join from
- >>>
- >>> # what i want:
- >>> # SELECT anon1.count AS anon1_count, child_b.someattr AS child_b_someattr
- >>> # FROM (SELECT child_a.parent_id AS child_a_parent_id, COUNT(child_a.id) AS count
- >>> # FROM child_a GROUP BY child_a.parent_id) AS anon1 LEFT OUTER JOIN child_b ON anon1.parent_id = child_b.parent_id
- >>>
- >>> # this demonstrates select_from is ignored
- >>> query3 = session.query(
- >>> ChildB.someattr,
- >>> sub.c.count,
- >>> )
- >>> query3 = query3.select_from(sub)
- >>> print(query3.outerjoin((ChildB, sub.c.parent_id == ChildB.parent_id)))
- SELECT child_b.someattr AS child_b_someattr, anon1.count AS anon1_count,
- FROM (SELECT child_a.parent_id AS child_a_parent_id, COUNT(child_a.id) AS count
- FROM child_a GROUP BY child_a.parent_id) AS anon1, child_b LEFT OUTER JOIN child_b ON anon1.parent_id = child_b.parent_id
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement