title: SQLAlchemy Session.merge() without accessing the database
body: <p>Our use case:
  Our REST endpoint gathers information from multiple hosts. For each host there's a row in a MySQL table. When a host sends new data, we update a metadata tablet with the latest update timestamp, and the row of the host, then commit.</p>
  
  <p>This is all done with <code>SQLAlchemy.Session.merge()</code>. This script has been working fine until now, but we've discovered that response time highly increases when the database is far away from the REST endpoint.</p>
  
  <p>With some investigation we've discovered that for each host information update the <code>merge(object)</code> calls connect the database, and then at <code>commit()</code> as well.</p>
  
  <p>I tried using <code>add(object)</code>, but that will fail because of the primary key collision (the host name is the primary key).</p>
  
  <p>I tried using <code>merge(object, load=False)</code> but that doesn't seem to work either.</p>
  
  <p>What I want is: update the local version of the object with the merge calls, then update the database on commit, updating on existing primary key, and inserting if primary key does not exist. Is this somehow possible?</p>
  
  <p>Some examples:</p>
  
  <pre><code>db.session.merge(object1)
  db.session.merge(object2)
  db.session.commit()
  </code></pre>
  
  <p>Accesses the database 3 times.</p>
  
  <pre><code>db.session.add(object1)
  db.session.add(object2)
  db.session.commit()
  </code></pre>
  
  <p>Fails, because there's already an <code>object1</code> and <code>object2</code> in the database (<code>IntegrityError</code>)</p>
  
  <pre><code>db.session.merge(object1, load=False)
  db.session.merge(object2, load=False)
  db.session.commit()
  </code></pre>
  
  <p>Also fails, because object1 and object2 are just created, which is not what the session expects (they have to be in the session).</p>
  
  <p>What I would like to have:</p>
  
  <pre><code>db.session.upsert(object1)
  db.session.upsert(object2)
  db.session.commit()
  </code></pre>
  
  <p>With the database only being accessed at commit, 1 time. <code>upsert</code> of course does not exist, I just invented the name.</p>