Skip to content

Recursive self join #110

@CmpHDL

Description

@CmpHDL

Lets say we had the following

Generated model

package model

type Categories struct {
	ID               int64 
	Name             *string
	ParentCategoryID *int64
}


User defined model

type Category struct {
	model.Categories

	Subcategories []Category
}

Is it currently possible to-do a recursive select from a categories table into model with a property of type self (Category)?
To where:

----------ID, Name, Parent
Category: 1, one, null
Category: 2, two, 1
Category: 3, three, 2
Category: 4, four, 1

would have the struct of

[
  {
    "id": 1,
    "name": "one",
    "parent_id": null,
    "subcategories": [
      {
        "id": 2,
        "name": "two",
        "parent_id": 1,
        "subcategories": [
          {
            "id": 3,
            "name": "three",
            "parent_id": 2,
            "subcategories": []
          }
        ]
      },
      {
        "id": 4,
        "name": "four",
        "parent_id": 1,
        "subcategories": []
      }
    ]
  }
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions