title: SQLAlchemy one to many query output
body: <p>I need to get a list of <code>planes</code> and all of their corresponding <code>bookings</code>, ideally with a list of planes where each plane has a list of bookings as one of it's properties so it can be easily json encoded. How can I do this with SQLAlchemy? I suspect SQLAlchemy has some powerful features to make this easy, I'd rather not have to do 2 queries and process the results.</p>
  
  <p>Eg result:</p>
  
  <pre><code>Plane
  - id:       -- 1
  - name:     -- Eagle Bravo
  - bookings: -- Booking1, Booking2, Booking3
  
  Plane
  - id:       -- 2
  - name:     -- Charlie Bravo
  - bookings: -- Booking4, Booking5, Booking6
  
  Plane
  - id:       -- 3
  - name:     -- Alpha Bravo
  - bookings: -- Booking7, Booking8, Booking9
  </code></pre>
  
  <p>Plane and Booking models:</p>
  
  <pre><code>class Booking(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      start_date = db.Column(db.DateTime)
      end_date = db.Column(db.DateTime)
      person_id = db.Column(db.Integer, db.ForeignKey('person.id'))
      person = db.relationship('Person', backref=db.backref('Bookings', lazy='dynamic'))
      plane_id = db.Column(db.Integer, db.ForeignKey('plane.id'))
  
      def __init__(self, start_date, end_date, person_id):
          self.start_date = start_date
          self.end_date = end_date
          self.person_id = person_id
  
      def __repr__(self):
          return '&lt;Booking... %r&gt;'
  
      def as_dict(self):
         return {c.name: getattr(self, c.name) for c in self.__table__.columns}
  
  
  class Plane(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(80))
  
      def __init__(self, name):
          self.name = name
  
      def __repr__(self):
          return '&lt;Plane... %r&gt;'
  
      def as_dict(self):
         return {c.name: getattr(self, c.name) for c in self.__table__.columns}
  </code></pre>