It should be the following query
-generating code.
sender = aliased(User)
recipient = aliased(User)
query = (
select(Message)
.join(sender, Message.id_from == sender.id)
.join(recipient, Message.id_to == recipient.id)
.options(contains_eager(Message.sender.of_type(sender)),
contains_eager(Message.recipient.of_type(recipient)))
.order_by(direction(sender.id))
)
Found it here