title: confusion about synchronization of SQLAlchemy objects with database
body: <p>I'm going through the SQLAlchemy ORM tutorial (<a href="https://docs.sqlalchemy.org/en/latest/orm/tutorial.html" rel="nofollow noreferrer">https://docs.sqlalchemy.org/en/latest/orm/tutorial.html</a>) and I'm finding it super difficult to understand when/why a Python object will reflect the latest data in the database. </p>
  
  <p>Here is a sequence of events that confuses me:</p>
  
  <ul>
  <li>First we create a user <code>ed_user</code> and add it to the session. Its <code>id</code> is <code>None</code> because the row hasn't been written to the database.</li>
  <li>Then we create a different user <code>our_user</code> which is obtained by querying the database with a query that will match <code>ed_user</code>. So <code>our_user</code> and <code>ed_user</code> are actually the same user. When we query either <code>our_user.id</code> or <code>ed_user.id</code> after this query has taken place, we see that the <code>id</code> has now been assigned because <code>ed_user</code> was flushed to the database when the SELECT query was written.</li>
  <li>Then we edit <code>ed_user</code> and add some other unrelated rows, and issue a session commit.</li>
  <li>Finally we then read the value of <code>ed_user.id</code> again and it causes the database to issue a SELECT query to get the latest value of id since the previous commit ended the previous transaction.</li>
  </ul>
  
  <p>I find this extremely confusing because in the first step, before <code>ed_user</code> was ever written to the database, SQLAlchemy was content to give us a <code>None</code> value for <code>id</code> <strong>even though it could have obtained an id if it went ahead and flushed the write to the database</strong> but for some reason once the row was written to the database once, SQLAlchemy thinks it is important to keep it up-to-date (in the last step) by refreshing the data when it is read. Why is this happening and what controls this behavior?</p>
  
  <p>Bottom line, I have no idea what logic I can rely on regarding when/why/how my Python objects will be kept up-to-date with the database, and any extra clarity you can offer will be extremely appreciated.</p>