-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recursive self join #110
Comments
Arbitrary depth self join is not possible. It is possible to scan if number of self join is predefined. One self join example - wiki.
type Category struct {
model.Categories
Subcategories []struct {
model.Categories `alias:"subcategories.*"`
}
} Similarly 2 level deep categories: type Category struct {
model.Categories
Subcategories1 []struct {
model.Categories `alias:"subcategories1.*"`
Subcategories2 []struct {
model.Categories `alias:"subcategories2.*"`
}
}
} Note that each |
This is one of the solutions I came to as well, but its just too terrible to deal with code wise. Also the depth does have a limit, but not all branches go to that depth. For anyone reading I scanned them into a flat slice then created a recursive function to create it into a tree based off the parent_id. (within go itself) @go-jet What are you thoughts about creating a view within the database todo the recursive part then doing a SELECT from said view? How would we get that into models without having multiple levels of struct within the model definition, if even possible since we would probably we running into the same problem of cyclic type. |
At the moment, I don't see any other way, except to use recursive with statement, and then manually group the result. Query methods can't help with this kind of result set. |
Yeah I was battling different implementations this morning and also tried something similar with rows.Next, fastest implementation still seems to be a custom recursive function to build the tree, after optimizing that (thousands of rows) the speed more bearable. I'll mess with your WITH RECURSIVE, love your speed with ideas / features. |
Lets say we had the following
Generated model
User defined model
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
The text was updated successfully, but these errors were encountered: