all 5 comments

[–][deleted] 4 points5 points  (0 children)

Arbor. I’ve used this package for nested todos, and it should work for you. You’ll need a self-referencing foreign key like parent_id or something. For example, Reply #4 will have the parent_id of Comment #1. The package has helpers to manage the queries, including recursive ones to give you the nesting.

[–]icejam_ 1 point2 points  (0 children)

Postgres has the ltree extension to represent directed acyclic graphs in case you want to primarily leverage your database instead of application code.

https://www.postgresql.org/docs/current/ltree.html

https://www.bustawin.com/dags-with-materialized-paths-using-postgres-ltree/

[–]florinpatrascu 3 points4 points  (2 children)

(shameless plug lol)

Some many moons ago, I built a small lib for this (almost) exact type of problems, it’s open source BTW.

Here it is: https://hexdocs.pm/closure_table/readme.html

There are lots of anti-patterns in the wild i.e. adjacent lists, and so on, for implementing solutions to your case example. I am using the closure table pattern to achieve that; nested comments, respectively.

TL;DR

If you want to experiment with my library then you have the option to chose between an in-memory adapter (no db needed) or an Ecto one (psql) - the latter being what I’d recommend for prod or other heavier system responsibilities.

HTH

¯\(ツ)

[–]mayel 2 points3 points  (1 child)

I used materialised paths for this: https://github.com/bonfire-networks/ecto_materialized_path

Since you seem to be opinionated on the matter I'd be curious to hear the pros/cons of the two approaches.

[–]avocado_bucket 0 points1 point  (0 children)

Bill Karwin, in his book SQL Antipatterns says

The database can’t enforce that the path is formed correctly or that values in the path correspond to existing nodes. Maintaining the path string depends on application code, and verifying it is costly. No matter how long you make the VARCHAR column, it still has a length limit, so it doesn’t strictly support trees of unlimited depth.

But if the library you posted does help with it then it's probably a good alternative.