title: Python SQLAlchemy: Reflecting the database breaks default/onupdate methods?
body: <p>I have two separate SQLAlchemy interfaces to a Postgres database. The first interface, in the context of a Flask App, contains this model:</p>
  
  <pre><code>app = create_app() # sets the SQLAlchemy Database URI, etc.
  db = SQLAlchemy(app)
  
  class User(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
      updated_at = db.Column(db.DateTime, onupdate=datetime.datetime.utcnow)
      name = db.Column(db.String, nullable=False)
  </code></pre>
  
  <p>The second interface is not through Flask -- rather, it's a script that listens for a particular event, in which case it is meant to perform some computations and update a row in the database. To accomplish this, I have SQLAlchemy reflect the existent database:</p>
  
  <pre><code>from sqlalchemy import create_engine, MetaData, Table
  from sqlalchemy.orm import mapper, sessionmaker
  from sqlalchemy.ext.automap import automap_base
  from os import environ
  
  dbPath = "postgresql://" + ...
  engine = create_engine(dbPath)
  
  Base = automap_base()
  Base.prepare(engine, reflect=True)
  
  metadata = MetaData(engine)
  
  class User(object):
      pass
  
  users = Table('user', metadata, autoload=True, autoload_with=engine)
  mapper(User, users)
  
  Session = sessionmaker(bind=engine)
  session = Session()
  </code></pre>
  
  <p>The issue I'm now running into is this: when I'm using the first interface to create a new entry or update one, things work as expected, and the <code>created_at</code> and <code>updated_at</code> fields are updated appropriately.</p>
  
  <p>However, when I'm using the second interface -- importing the code and using <code>session.query(User)</code> to get an entry and to update it, the <code>updated_at</code> field doesn't change. Moreover, when I'm using this interface to create a new User, while it creates the new row as expected, it populates neither the <code>created_at</code> nor <code>updated_at</code> fields.</p>
  
  <p>My questions:</p>
  
  <ol>
  <li>Why is this happening? Why does the reflection seemingly break the <code>default/onupdate</code> methods?</li>
  <li>How can I fix this? </li>
  </ol>