title: SQLAlchemy: Multiple foreign keys to same table with compound primary key
body: <p>In SQLAlchemy, imagine we have a table <code>Foo</code> with a compound primary key, and <code>Bar</code>, which has two foreign key constrains linking it to <code>Foo</code> (each <code>Bar</code> has two <code>Foo</code> objects).</p>
  
  <p>My problem is with the <code>relationship</code> function, which makes me repeat the information (in the <code>primaryjoin</code>) which I have already given in the <code>ForeightKeyConstraint</code> (violating DRY). Is there some other way of structuring this so that I don't have to repeat information? Some way of passing the relevant <code>ForeignKeyConstraint</code> to <code>relationship</code>?</p>
  
  <pre><code>class Foo(Base):
      __tablename__ = "Foo"
      id_1 = Column(Integer, primary_key=True)
      id_2 = Column(Integer, primary_key=True)
  
  class Bar(Base):
      __tablename__ = "Bar"
      id = Column(Integer, primary_key=True)
  
      foo_1_id_1 = Column(Integer)
      foo_1_id_2 = Column(Integer)
  
      foo_2_id_1 = Column(Integer)
      foo_2_id_2 = Column(Integer)
  
      __table_args__ = (
              ForeignKeyConstraint(
                  [foo_1_id_1,foo_1_id_2],
                  [Foo.id_1,Foo.id_2]
                  ),
              ForeignKeyConstraint(
                  [foo_2_id_1,foo_2_id_2],
                  [Foo.id_1,Foo.id_2]
                  )
              )
  
      foo_1 = relationship(Foo,primaryjoin="(Bar.foo_1_id_1 == Foo.id_1) &amp; (Bar.foo_1_id_2 == Foo.id_2)")
      foo_2 = relationship(Foo,primaryjoin="(Bar.foo_2_id_1 == Foo.id_1) &amp; (Bar.foo_2_id_2 == Foo.id_2)")
  </code></pre>
  
  <p>Thanks.</p>