Advertisement
danielepaolella

Untitled

Oct 12th, 2011
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. >>> sub = session.query(
  2. >>>     ChildA.parent_id,
  3. >>>     func.count(ChildA.id).label('count'),
  4. >>> )
  5. >>> sub = sub.group_by(ChildA.parent_id)
  6. >>> sub = query.subquery()
  7. >>>
  8. >>> # this works
  9. >>> query1 = session.query(
  10. >>>     ChildB.someattr,
  11. >>>     sub.c.count,
  12. >>> )
  13. >>> query1 = query1.select_from(ChildB)
  14. >>> print(query1.outerjoin((sub, sub.c.parent_id == ChildB.parent_id)))
  15. SELECT child_b.someattr AS child_b_someattr, anon1.count AS anon1_count
  16. FROM child_b LEFT OUTER JOIN (SELECT child_a.parent_id AS child_a_parent_id, COUNT(child_a.id) AS count
  17. FROM child_a GROUP BY child_a.parent_id) AS anon1 ON anon1.parent_id = child_b.parent_id
  18. >>>
  19. >>> # this doesn't
  20. >>> query2 = session.query(
  21. >>>     sub.c.count,
  22. >>>     ChildB.someattr,
  23. >>> )
  24. >>> query2 = query2.select_from(sub)
  25. >>> print(query2.outerjoin((ChildB, sub.c.parent_id == ChildB.parent_id)))
  26. Traceback (most recent call last):
  27.     ...
  28. InvalidRequestError: Could not find a FROM clause to join from
  29. >>>
  30. >>> # what i want:
  31. >>> # SELECT anon1.count AS anon1_count, child_b.someattr AS child_b_someattr
  32. >>> # FROM (SELECT child_a.parent_id AS child_a_parent_id, COUNT(child_a.id) AS count
  33. >>> # FROM child_a GROUP BY child_a.parent_id) AS anon1 LEFT OUTER JOIN child_b ON anon1.parent_id = child_b.parent_id
  34. >>>
  35. >>> # this demonstrates select_from is ignored
  36. >>> query3 = session.query(
  37. >>>     ChildB.someattr,
  38. >>>     sub.c.count,
  39. >>> )
  40. >>> query3 = query3.select_from(sub)
  41. >>> print(query3.outerjoin((ChildB, sub.c.parent_id == ChildB.parent_id)))
  42. SELECT child_b.someattr AS child_b_someattr, anon1.count AS anon1_count,
  43. FROM (SELECT child_a.parent_id AS child_a_parent_id, COUNT(child_a.id) AS count
  44. 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
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement