title: How to "resolve" foreign keys in SQLAlchemy query results?
body: <p>I've written a small tool that creates SQLAlchemy code from a written specification. The tool can create code like this:</p>
  
  
  
  <pre class="lang-python prettyprint-override"><code>class Person(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(50))
      address_id = db.Column(db.Integer, db.ForeignKey('address.id'))
  
  class Address(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      email = db.Column(db.String(50))
  </code></pre>
  
  <p>I can insert new records, I can query them, that works like a charm, but I would really like to not having to manually look for the Address of a Person.</p>
  
  <p>I want to be able to access the address belonging to a specific person through the Person object. If I understand the examples correctly I need to do it like this (example based on <a href="http://flask-sqlalchemy.pocoo.org/2.1/models/#one-to-many-relationships" rel="nofollow noreferrer">http://flask-sqlalchemy.pocoo.org/2.1/models/#one-to-many-relationships</a>)?</p>
  
  <pre class="lang-python prettyprint-override"><code>class Person(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(50))
      address = db.relationship('Address', backref='person',
                                  lazy='dynamic')
  
  class Address(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      email = db.Column(db.String(50))
      person_id = db.Column(db.Integer, db.ForeignKey('person.id'))
  </code></pre>
  
  <p>This looks to me all backwards, I have to give every Address a "link" to a Person and then backref so I can have an addresses for a person? Is this not possible to do directly from Person? </p>
  
  <p>Also, creating code for this is much more complicated, so I'd really like to avoid that.</p>