Laetitia
How to model nested categories?
Hi everybody,
I want to implement a system with categories and subcategories and subsub and subsubsub… lol.
ie: the category A has 2 subcategories: B and C and C have a sub-category D.
The deep of the nesting is not rigid and may grow.
How would you model this, allowing easy search in db? Search ancestor and children? Is there a way to avoid recursion?
Most Liked
al2o3cr
Postgres can do recursive common-table expressions if you really want, but it’s worth considering carefully exactly how your application will use the nested categories.
- how often are new categories added? Some representations will make this expensive
- how often do queries like “in this category and all its subcategories” occur? Some representations will make THAT expensive
Poking around in Hex turns up some candidates:
-
Arbor uses CTEs to traverse
parent_idchains - Ancestry uses materialized paths; each record stores the full “path” through the hierarchy (a record in Subsubcategory D would store “A/C/D” or similar)
-
as_nested_setuses nested sets to make answering “and all subcatgories” questions faster - Hierarch linked up-thread by @fuelen uses a Postgres extension to offload some of the complexity to the database
fuelen
Hello
Did you see this library https://github.com/Byzanteam-Labs/hierarch?
It is based on ltree postgres extension. I didn’t try it, but I’d try if I have this kind of requirements 
kokolegorille
Another way is to use a nested set model, using lft and rgt additional columns.
l00ker
I just did this for a project I’ve been working on. I ended up settling on closure_table.
The biggest challenge was building the proper data structure required by the admin front-end widget to use drag-n-drop etc.
dimitarvp
These are the famous last words written on the tombstones of countless never-released projects though… ![]()
It will never be perfect. Post it on GitHub when you get a free hour some evening. People like me might like it and offer PRs.
Don’t be obsessed with a theoretical perfect state of your code. I know exactly how you feel but we the techies should apply some business thinking to our own voluntary work here and there: if it works and it’s good enough then it’s very likely just fine for presentation as well.
You don’t want to know how god-awful my Rust code in my sqlite3 Elixir<=>Rust library is. But it does what it’s supposed to for the moment. Overcoming analysis paralysis and the desire for perfection are skills that will put you above many programmers.
At least from my side I can promise not to judge. Only to send PRs if I feel that something can be improved.







