title: SQLAlchemy update parent when related child changes
body: <p>I'm trying to model an entity that as one or more one-to-many relationships, such that it's <code>last_modified</code> attribute is updated, when</p>
  
  <ul>
  <li>a child is added or removed</li>
  <li>a child is modified</li>
  <li>the entity itself is modified</li>
  </ul>
  
  <p>I've put together the following minimal example:</p>
  
  <pre><code>class Config(Base):
      __tablename__ = 'config'
  
      ID = Column('ID', Integer, primary_key=True)
      name = Column('name', String)
      last_modified = Column('last_modified', DateTime, default=now, onupdate=now)
  
      params = relationship('ConfigParam', backref='config')
  
  class ConfigParam(Base):
  
      __tablename__ = 'config_params'
  
      ID = Column('ID', Integer, primary_key=True)
      ConfigID = Column('ConfigID', Integer, ForeignKey('config.ID'), nullable=False)
  
      key = Column('key', String)
      value = Column('value', Float)
  
  @event.listens_for(Config.params, 'append')
  @event.listens_for(Config.params, 'remove')
  def receive_append_or_remove(target, value, initiator):
      target.last_modified = now()
  
  @event.listens_for(ConfigParam.key, 'set')
  @event.listens_for(ConfigParam.value, 'set')
  def receive_attr_change(target, value, oldvalue, initiator):
      if target.config:
          # don't act if the parent config isn't yet set
          # i.e. during __init__
          target.config.last_modified = now()
  </code></pre>
  
  <p>This seems to work, but I'm wondering if there's a better way to do this?</p>
  
  <p>Specifically, this becomes very verbose since my actual <code>ConfigParam</code> implementation has more attributes and I'm having multiple one-to-many relations configured on the parent <code>Config</code> class.</p>