I recently needed a performant way to manage a tree, meaning using MPTT, nested sets etc.
I looked at the following libraries, which integrate with SQLAlchemy. These are my notes, some of which seem to be outdated now.
https://github.com/ITCase/sqlalchemy_mptt/
No features for querying- Injects fields like
parent_id
,children
into the model by itself, without allowing customization. - It’s
rebuild()
is not able to init a tree from scratch.
https://sqlalchemy-orm-tree.readthedocs.org/
Integrating into the class-based ORM mapper is messy(pr accepted)No(pr accepted)rebuild()
feature at all- Has bugs when used with ORM attributes are diverging from table column names
- BIG PLUS: can detect and handle tree moves if attributes on an existing node change.
- Does not support keeping a sort order around.
https://bitbucket.org/angri/sqlamp/
- Seems to have a working
rebuild()
- Can sort of integrate with the class-based ORM mapper setup (although it seems it would need to be globally)
tree_recursive_iterator()
for querying a tree.- To move a subtree, we need to call special
move()
methods.
sqlamp
and sqlalchemy-orm-tree
seem to share a lot of ideas/APIs.
sqlamp
seems to be the most most stable / actively developed, although sqlalchemy-orm-tree
makes a good impression, too, and the tree move detection is so helpful that I decided to go with it.