I'm trying to implement a nested set model using Flask-SQLAlchemy.
I see this example using SQLAlchemy: http://docs.sqlalchemy.org/en/rel_0_9/_modules/examples/nested_sets/nested_sets.html
The magic seems to happen in their definition of before_insert
:
@event.listens_for(Employee, "before_insert")
def before_insert(mapper, connection, instance):
if not instance.parent:
instance.left = 1
...
I'm definitely not an expert with nested sets, but as I understand them, an "add" requires some pre-work to figure out new values for "left" and "right" for both the new item to be added, as well as potentially for everything in the table. (Similarly for remove.) I suppose maybe I could just add that pre-work in the regular flow within my app, and not use the before_insert
from the example.
I've been having trouble finding if there's a way for me to override the db.session.add
from within the Flask-SQLAlchemy framework in a similar way. Or should I be doing something else here?