uralbash / sqlalchemy_mptt

SQLAlchemy nested sets mixin (MPTT)
http://sqlalchemy-mptt.readthedocs.io
MIT License
196 stars 32 forks source link

better initialization of empty tree #54

Open dshlai opened 7 years ago

dshlai commented 7 years ago

Hi!

In order to initialize an empty tree. I used the following code based on the example in the doc:

    # if the table is empty
    if not current_session.query(TreeDelcaretiveClass).all():
        tree_manager.register_events(remove=True)
        # Hard code the tree_id since I know this is the first node and the only root node in the current tree
        root = TreeDelcaretiveClass(name="Root", tree_id=1)
        # Hard code the left since I know the initial value for the left must be 1
        root.left = 1
        # Hard code the right since I know the initial value for the right must be 2
        root.right = 2
        current_session.add(root)
        current_session.commit()

        tree_manager.register_events()
        TreeDeclaretiveClass.rebuild_tree(current_session, TreeDeclaretive.tree_id)

Maybe this can be a function in the mixin module that help user initialize the empty tree? With at least one item in the tree (even if it is not used), tree will be very easy to use afterward.