Diesel - how to use recursive ORM model?


#1

I just want to start with the fact that I’m quite new to web dev, so I’m not too well acquainted with how to achieve what I want.
I want to create an SQL db with diesel ORM. One of the tables will contain something called groups which can be thought of as a hierarchical tree structure.

E.g. a group can be root group, or it can have a single parent. Each group should have a unique_id and a user who owns it and a potential parent. My question is how to create this struct in Rust such that diesel would be able to for instance fetch me the whole tree below a given group? Also how would I actually execute such form of recursive invocation in diesel if that is possible at all?

Any help in this regard would be most helpful, however I did not see in any if diesel examples of how to actually work with such tables.


#2

What you describe is a tree storage scheme known as the adjacency list. A far more efficient hierarchical storage scheme for databases is the nested set model. There’s a large amount of information available online about it, e.g. http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/. Diesel does not have any convenience methods to deal with nested sets but the queries described in that article should be pretty easy to write with Diesel.


#3

Thanks, such article was what I was looking for.