title: SQLAlchemy query for object with count of relationship
body: <p>Lets say I have SQL Alchemy ORM classes:</p>
  
  <pre><code>class Session(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_agent = db.Column(db.Text, nullable=False)
  
  class Run(db.Model):
    id = db.Column(db.Integer, primary_key=True)
  
    session_id = db.Column(db.Integer, db.ForeignKey('session.id'))
    session = db.relationship('Session', backref=db.backref('runs', lazy='dynamic'))
  </code></pre>
  
  <p>And I want to query for essentially the following:</p>
  
  <pre><code>((session.id, session.user_agent, session.runs.count())
    for session in Session.query.order_by(Session.id.desc()))
  </code></pre>
  
  <p>However, this is clearly 1+n queries, which is terrible. What is the correct way to do this, with 1 query? In normal SQL, I would do this with something along the lines of:</p>
  
  <pre><code>SELECT session.id, session.user_agent, COUNT(row.id) FROM session
  LEFT JOIN rows on session.id = rows.session_id
  GROUP BY session.id ORDER BY session.id DESC
  </code></pre>