Use of label in sqlalchemy , database, ORM

Viewed 5063
q = session.query(
    label('id1', func.least(Communication.initiator_id, Communication.receiver_id)),
    label('id2', func.greatest(Communication.initiator_id, Communication.receiver_id)),
    label('orga_id1', func.greatest(0, 1)),
    label('orga_id2', func.greatest(0, 1)),
    label('nb', func.count(Communication.id))
).filter(
    or_(
        Communication.initiator_id == people_id,
        Communication.receiver_id == people_id
    )
).order_by("nb").group_by('id1', 'id2').all()

So i have this piece of code, and it's working fine but only one thing i would like to add here, and that thing is the use of labels. For Exemple how do i use the name label "id1" in my other label?

I my code to be something like this:

q = session.query(
    label('id1', func.least(Communication.initiator_id, Communication.receiver_id)),
    label('id2', func.greatest(Communication.initiator_id, Communication.receiver_id)),
    label('orga_id1', func.greatest(session.query(People).filter_by(id = "And here somehow i want the value that was created on first label with name 'id1'").first().orga_id, -1)),
    label('orga_id2', func.greatest(session.query(People).filter_by(id = "And here somehow i want the value that was created on second label with name 'id2'").first().orga_id, -1)),
    label('nb', func.count(Communication.id))
).filter(
    or_(
        Communication.initiator_id == people_id,
        Communication.receiver_id == people_id
    )
).order_by("nb").group_by('id1', 'id2').all()

Can someone pls explain me how do i use the value of labels inside the query !!!

1 Answers
Related