Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When using SQLAlchemy in a Flask app with <a href="http://packages.python.org/Flask-SQLAlchemy/" rel="nofollow noreferrer">Flask-SQLAlchemy</a> it provides a <a href="http://packages.python.org/Flask-SQLAlchemy/signals.html#models_committed" rel="nofollow noreferrer">models_committed</a> signal which receives a list of <code>(model, operation)</code> tuples. Using this signal doing what I'm looking for is extremely easy:</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, '__commit_delete__'): obj.__commit_delete__() </code></pre> <p>With this generic function every model that needs on-delete-commit code now simply needs to have a method <code>__commit_delete__(self)</code> and do whatever it needs to do in that method.</p> <hr> <p>It can also be done without Flask-SQLAlchemy, however, in this case it needs some more code:</p> <ul> <li>A deletion needs to be recorded when it's performed. This is be done using the <a href="http://docs.sqlalchemy.org/en/latest/orm/events.html#sqlalchemy.orm.events.MapperEvents.after_delete" rel="nofollow noreferrer"><code>after_delete</code> event</a>.</li> <li>Any recorded deletions need to be handled when a COMMIT is successful. This is done using the <a href="http://docs.sqlalchemy.org/en/latest/orm/events.html#sqlalchemy.orm.events.SessionEvents.after_commit" rel="nofollow noreferrer"><code>after_commit</code> event</a>.</li> <li>In case the transaction fails or is manually rolled back the recorded changes also need to be cleared. This is done using the <a href="http://docs.sqlalchemy.org/en/latest/orm/events.html#sqlalchemy.orm.events.SessionEvents.after_rollback" rel="nofollow noreferrer">after_rollback()</a> event.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload