title: SQLAlchemy: how to create a relationship programmatically
body: <p>I'd like to create a 1:n relationship between two tables dynamically. My DB model is mapped via SQLAlchemy but due to some special features of my application I can not use the default declarative way.</p>
  
  <p>E.g.</p>
  
  <pre><code>class Foo(Base):
      id = Column(Integer, autoincrement=True, primary_key=True)
      flag = Column(Boolean)
  
  class Bar(Base):
      id = Column(Integer, autoincrement=True, primary_key=True)
      foo_id = Column(Integer, ForeignKey('foo.id'))
      # declarative version:
      # foo = relationship(Foo)
  </code></pre>
  
  <p>So I want to add relationship named "foo" to the mapped class "Bar" <strong>after</strong> Bar was defined and SQLAlchemy did its job of defining a mapper etc.</p>
  
  <p><strong>Update 2017-09-05:</strong> Why is this necessary for me? (I thought I could omit this because I think it mostly distracts from the actual problem to solve but since there were comments abouts it...)</p>
  
  <p>First of all I don't have a single database but hundreds/thousands. Data in old databases must not be altered in any way but I want a single source code to access old data (even though data structure and calculation rules change significantly).</p>
  
  <p>Currently we use multiple model definitions. Later definitions extend/modify previous ones. Often we manipulate SQLAlchemy models dynamically. We try not to have code in mapped classes because we think it will be much harder ensuring correctness of that code after changing a table many times (code must work in every intermediate step). </p>
  
  <p>In many cases we extend tables (mapped classes) programatically in model X after it was initially defined in model X-1. Adding columns to an existing SQLAlchemy ORM class is manageable. Now we are adding a new reference column an existing table and a <code>relationship()</code> provides a nicer Python API.</p>