On Tue, Mar 19, 2019 at 10:23 AM kosta <
naum...@gmail.com> wrote:
>
> Hello Mike,
> Thank you for your response!
>
>
> First of all, I'm apologize, I have lack knowledge in sql. I guess my SQL should be as:
> Get all users invited by specific user:
>
> SELECT
u.name AS "sender",
i.name AS "invitee"
> FROM invitation inv
> LEFT JOIN user u ON inv.sender_id=
u.id
> LEFT JOIN user i ON inv.invitee_id=
i.id
> WHERE inv.sender_id=?;
>
> Get user who invited specific user:
>
> SELECT
u.name AS "invited_by",
i.name AS "invitee"
> FROM invitation inv
> LEFT JOIN user u ON inv.sender_id=
u.id
> LEFT JOIN user i ON inv.invitee_id=
i.id
> WHERE inv.invitee_id=?;
here's the form of the first one and the second is basically the same idea:
from sqlalchemy.orm import aliased
u = aliased(User, "u")
i = aliased(User, "i")
q = session.query(
u.name.label("sender"),
i.name.label("invitee")
).select_from(Invitation).\
outerjoin(u, Invitation.sender).\
outerjoin(i, Invitation.invitee).\
filter(Invitation.sender_id=5)