Try contains_eager instead of joinload. Most likely, what happens is that you have 4 connections with two that you defined using the join, and then two of the parameters (joinload (...))
By changing your code, you should give the following:
from sqlalchemy import and_ result = (session.query(Host).filter(and_(Host.id.in_(ids), Host.deleted == False)). join(Switch, and_(Switch.host_id==Host.id, Switch.deleted == False)). join(Port, and_(Port.switch_id==Switch.id, Port.deleted == False)). options(contains_eager('switches')). options(contains_eager('ports')). all() )
source share