title: SQLAlchemy: How to use an instance in SQLAlchemy after session.close()?
body: <p>I am having problems using an instance in SQLAlchemy after session.close().</p>
  
  <p>This is the code:</p>
  
  <pre><code>session = sqlalchemy.orm.sessionmaker(bind=engine)
  object = session.query(Entity).filter(Entity.id == id).one()
  object.name = "Foo"
  session.commit()
  session.close()
  print object.name
  </code></pre>
  
  <p>It throws a DetachedInstanceError.</p>
  
  <p>I have tried to detach the object, expunge_all() and nothing.</p>
  
  <p>Thanks</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I found out what was the problem. For future reference I will answer my own question.</p>
  
  <p>session.commit() sets the object instance as expired, when I try to access to the object attributes after the close() statement SQLAlchemy tries to refresh the instance.
  Calling refresh after commit() solves the problem.</p>