title: Flask-SQLAlchemy and Third Normal Form
body: <p>How do I achieve 3rd normal form, like having a seperate <code>Address</code> model in Flask-SQLAlchemy? The joker is, that I would like to be able to use this seperate model from multiple different models. </p>
  
  <p>For instance, let's say I have two different models, a <code>Person</code> and a <code>Company</code>:</p>
  
  <pre><code>class Person(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      street_address = db.Column(db.Text, nullable=False)
      postal_code = db.Column(db.Text, nullable=False)
      city = db.Column(db.Text, nullable=False)
      country = db.Column(db.Text, nullable=False)
      # ...and so on
  
  class Company(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      street_address = db.Column(db.Text, nullable=False)
      postal_code = db.Column(db.Text, nullable=False)
      city = db.Column(db.Text, nullable=False)
      country = db.Column(db.Text, nullable=False)
      # ...and so on
  </code></pre>
  
  <p>Now this is just silly. An address does not depend on whether it's a Person or a Company. So I can make a seperate model, like this:</p>
  
  <pre><code>class Address(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      street_address = db.Column(db.Text, nullable=False)
      postal_code = db.Column(db.Text, nullable=False)
      city = db.Column(db.Text, nullable=False)
      country = db.Column(db.Text, nullable=False)
  </code></pre>
  
  <p>And then change my person model to be like this:</p>
  
  <pre><code>class Person(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      address_id = db.Column(db.Integer, db.ForeignKey('address.id'), nullable=False)
      # ...and so on
  </code></pre>
  
  <p>And then add a back reference to my <code>Address</code> model:</p>
  
  <pre><code>class Address(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      street_address = db.Column(db.Text, nullable=False)
      postal_code = db.Column(db.Text, nullable=False)
      city = db.Column(db.Text, nullable=False)
      country = db.Column(db.Text, nullable=False)
      person = db.relationship('Person', backref='address', lazy=True) # &lt;&lt; NEW!
  </code></pre>
  
  <p>However, now I have a design which locks the <code>Address</code> to the <code>Person</code>. I want all models, which may need an address, to be able to use the model. How do I achieve this?</p>