I have multiple categories, which can have None or one or multiple sub-categories.
The process theoretically can go to infinite. So, it is like having multiple trees.
Tree example.
A
- A1
- A11
- A12
-A2
B
C
- C1
I have also Item(s). An Item can be in multiple categories.
At this moment to connect the categories, in database I use three fields:
children (the children of a category),
path([1,4,8], basically are the ids of grandparent, parent, and category itself)
depth, represent the level in the tree for each category
Using this fields I avoid some recursive and using more queries.
I usually retrieve data like:
top categories (depth 0)
subcategories of a category
sibling categories
items in the category (for example a grandparent category, will show its direct items, the items of children, and the items of grandchildren)
At this moment I'm using Django(want to move to FastAPI) and PostgreSQL, and every time I have CRUD operations on categories, the three fields (path,depth,children)will be modified.
I'm thinking maybe is a better way, to maintain/retrieve the categories tree and corresponding items.