title: sqlalchemy easy way to insert or update?
body: <p>I have a sequence of new objects. They all look like similar to this:</p>
  
  <p>Foo(pk_col1=x, pk_col2=y, val='bar')</p>
  
  <p>Some of those are Foo that exist (i.e. only val differs from the row in the db)
  and should generate update queries. The others should generate inserts.</p>
  
  <p>I can think of a few ways of doing this, the best being:</p>
  
  <pre><code>pk_cols = Foo.table.primary_key.keys()
  for f1 in foos:
      f2 = Foo.get([getattr(f1, c) for c in pk_cols])
      if f2 is not None:
          f2.val = f1.val # update
          # XXX do we need to do session.add(f2) 
          # (or at least keep f2 alive until after the commit?)
      else:
          session.add(f1) # insert
  
   session.commit()
  </code></pre>
  
  <p>Is there an easier way?</p>