title: SQLAlchemy relationships populating foreign key fields
body: <p>I have the following tables with their respective sqlalchemy classes:</p>
  
  <pre><code>class Enrolled(Base):
      __tablename__ = 'enrolled'
      id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
      student_fk = Column(Integer, ForeignKey('students.id'))
      student = relationship('Students', foreign_keys=[device_fk], uselist=False,backref="enrolled", innerjoin=False, post_update=False)
      subject = Column(String(5, convert_unicode=True), nullable=False)
  
  //__init__ for id and subject is here.
  
  class Students(Base):
      __tablename__ = 'students'
      id = Column(Integer, primary_key=True, nullable=False, autoincrement=True)
      name = Column(String(50, convert_unicode=True), nullable=False)   
  //init for name is here
  </code></pre>
  
  <p>Relationship between students and enrolled is one to many. i.e one student can enroll himself in more then 1 subject.</p>
  
  <p>Now, I know to insert a couple of subjects into 'Enrolled' and names into 'Students' classes.</p>
  
  <pre><code>DBSession.add(Enrolled(subject="maths"))
  </code></pre>
  
  <p>In the end this is how my tables look </p>
  
  <p>Enrolled:</p>
  
  <pre><code>+----+------------+---------+
  | id | student_fk | subject |         
  +----+------------+---------+
  |  1 |            | Maths   |
  |  2 |            | Physics | 
  |  3 |            |  Art    | 
  +----+------------+---------+
  </code></pre>
  
  <p>Students:</p>
  
  <pre><code>+----+------+
  | id | name |  
  +----+------+
  |  1 | Jim  | 
  |  2 | Bob  |  
  |  3 | Cara |  
  +----+------+
  </code></pre>
  
  <p>Now, how do I get the students id get into <strong>Enrolled</strong> table as foreign keys?</p>
  
  <p>I have this information : which student is enrolled into which subject as a .csv file..
  mycsv: name,subject,name1,subject1,name2,subject2</p>
  
  <p>Should I have a manual dictionary like dict {jim:maths,Bob:Art,Cara:Physics} and then map like</p>
  
  <pre><code>query=Enrolled(subject="maths")
  
  for k, v in dict.items():
      if subject in v:
          list.append(k)
  for i in list:
      query.student=DBSession.query(Students).filter(name=i).first()
      DBSession.add(query)
  </code></pre>
  
  <p>Please help.. How do I get the student_fk field populated properly?</p>