peopledoc / django-ltree-demo

A demo for storing and querying trees in Django using PostgreSQL
MIT License
94 stars 12 forks source link

Is `parent` field really needed? #3

Closed amaury1093 closed 5 years ago

amaury1093 commented 5 years ago

https://github.com/peopledoc/django-ltree-demo/blob/575225237977c010e19bb1f33a5dd764c7183946/demo/categories/models.py#L7-L8

Am i correct in saying that this field is not actually needed, since with a materialized path you already have the whole path to the root? It's just convenience field for to get each node's parent/children?

k4nar commented 5 years ago

With this demo you don't construct the path "by hand" but let PostgreSQL construct it for you, only based on who the parent is. So you actually need to store the parent's ID in order to let the DB do its stuff :) .

If you don't mind managing the path by yourself then, indeed, you don't need to store the parent. You also don't need the SQL triggers.

amaury1093 commented 5 years ago

Got it, thanks!