peopledoc / django-ltree-demo

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

Is the AFTER UPDATE trigger really needed? #4

Closed amaury1093 closed 5 years ago

amaury1093 commented 5 years ago

https://github.com/peopledoc/django-ltree-demo/blob/575225237977c010e19bb1f33a5dd764c7183946/demo/categories/sql/triggers.sql#L48-L54

I can't get my head around why the above trigger is needed. Imo it will do the same job again as the BEFORE UPDATE trigger.

Can you provide an example where the AFTER does something different than the BEFORE?

k4nar commented 5 years ago

This is the case covered by this test: https://github.com/peopledoc/django-ltree-demo/blob/741de07db550bc8db1e7086ed8cb3d3c443ffe7d/tests/test_ltree.py#L126-L153 . Basically this happens every time you change the parent of a row which has descendants.

The trigger category_path_update_trg only updates the path of the current row, but we need to update the path of all the descendants as well. This is what category_path_after_trg does.

amaury1093 commented 5 years ago

💯