title: SQLAlchemy - Update ForeignKey when setting the relationship
body: <p>I have a class:</p>
  
  <pre><code>class ExampleClass(Base):
      __tablename__ = 'chart'
      id = Column(Integer, primary_key=True)
      element_id = Column(Integer, ForeignKey('anotherTable.id'))
      element = relationship(AnotherClass)
      element2_id = Column(Integer, ForeignKey('anotherTable2.id'))
      element2 = relationship(AnotherClass2)
  </code></pre>
  
  <p>I want to do a lookup based on the <code>element_id</code> and <code>element2_id</code> :</p>
  
  <pre><code>class ExampleClass(Base):
      ...
      def get_with_element2(self, element2):
          return session.query(ExampleClass).\
                         filter_by(element_id = self.element_id,
                                   element2_id = element2.id).first()
  </code></pre>
  
  <p>The problem I find is that if I instantiate a new <code>ExampleClass</code> object and assign it an <code>element</code>, the <code>element_id</code> field is not being set:</p>
  
  <pre><code>a = ExampleClass(element=element_obj)
  a.element_id =&gt; None
  </code></pre>
  
  <p>How can I solve this? What's the best way to deal with this kind of situation?</p>