title: after_insert event on different table
body: <p>I have Table A and Table B where I'd like to add/update/delete columns in Table B on Table A events.  I've tried the following:</p>
  
  <ol>
  <li>Mapper Events a.k.a @event.listens_for(SomeClass, 'before_insert')</li>
  </ol>
  
  <p>The documentation states</p>
  
  <blockquote>
    <p>Mapper-level flush events are designed to operate on attributes local to the immediate object being handled and via SQL operations with the given Connection only... Operations that are not supported in mapper events include: Session.add() Session.delete()... Mapped relationship attribute set/del events</p>
  </blockquote>
  
  <p>This limits whatever I can do with other tables. For instance,</p>
  
  <pre><code>def after_insert_listener(mapper, connection, target):
      target.value = "New Value"
  </code></pre>
  
  <p>works perfectly fine while</p>
  
  <pre><code>def after_insert_listener(mapper, connection, target):
      target.child.count += 1
  </code></pre>
  
  <p>doesn't do the trick. Putting db.session.commit() gives me this error</p>
  
  <blockquote>
    <p>ResourceClosedError: This transaction is closed</p>
  </blockquote>
  
  <p>I tried using SessionEvents.before_flush(), but neither could it be attached to a particular model nor did I understand how to use it.</p>
  
  <ol start="2">
  <li>Flask-SQLAlchemy signals</li>
  </ol>
  
  <p>I tried using models_committed signal:</p>
  
  <pre><code>@models_committed.connect_via(app)
  def on_models_committed(sender, changes):
      for obj, change in changes:
          if change == 'delete' and hasattr(obj, '__after_delete__'):
              obj.__after_delete__()
          elif change == 'update' and hasattr(obj, '__after_update__'):
              obj.__after_update__()
          elif change == 'insert' and hasattr(obj, '__after_insert__'):
              obj.__after_insert__()
  
  class TableA(db.Model):
      def __after_insert__(self):
          self.child.count += 1
          self.value = "New Value"
          db.session.commit()
  </code></pre>
  
  <p>This gave me</p>
  
  <blockquote>
    <p>InvalidRequestError: This session is in 'committed' state; no further SQL can be emitted within this transaction.</p>
  </blockquote>
  
  <p>How can I update instances of a model after another model is inserted?</p>