pg
pg copied to clipboard
Recursively retrieve relations of the model?
Is there any way to get relations recursively? for example I've got the following struct:
type Tree struct {
ID int
Parent *Tree
ParentID string
Name string
}
and I need to get all children of the object recursively. For now I'm doing like this
trees := []Tree{}
db.Model(&trees).Relation("Parent").Relation("Parent.Parent").Where("parent_id is null").Select()
it works if there is only two levels of depth but what if I don't know the depth of the relation?
@latipovsharif edit to write beauty code please.
@frederikhors please look at this.
Try to google "postgres tree" if you need a tree. I don't think you should be using ORM to build it.