title: Proper way to insert records with foreign keys in sqlalchemy
body: <p>I created a number of classes in SQLAlchemy to represent my various tables. I now want to insert records into these tables from various csv files that contain the data in an unnormalized format. What is the best way to deal with foreign keys?</p>
  
  <p>In a simplified model, I have two tables: Child and Parent, with a one to many relationship. The parent table is already filled up, with a unique parent_name for each primary key. I am currently doing this:</p>
  
  <pre><code>for index, row in df.iterrows():
      u = session.query(Parent).filter_by(parent_name=row['parent_name']).first()
      session.add(Child(child_name=row['child_name'], parent_id=u.id))
  </code></pre>
  
  <p>Is there a way with sqlalchemy to avoid the first query? <a href="https://stackoverflow.com/questions/5610563/what-is-the-proper-way-to-insert-an-object-with-a-foreign-key-in-sqlalchemy">This question</a> implies that using relationships is the correct/easy way to do it, but only explains the hard way.</p>