Open garthreckers opened 1 year ago
@codeliger - Doesn't look like its necessary for a relationship. I have a lot of other working relationships without the leading comma and the documentation doesn't include it: https://bun.uptrace.dev/guide/relations.html#has-many-relation
Thanks for the suggestion though.
Have you tried renaming AllChildren to Categories, or specifying a table name in the definition?
I think the reason there is no table name defined is because it derives it from Profiles []*Profile `bun:"rel:has-many,join:id=user_id"
@codeliger - I tried changing it to Categories but it throws the same error:
*errors.errorString: bun: has-many relation=Categories does not have base model=Category with id=[%!q(*uint=0x40004397c0)] (check join conditions)
When you say specify a table name, where would that be added? I have a table definition in the BaseModel.
From my debugging, it makes the correct database query since I can see the relationship query when I run it in debug mode:
SELECT `category`.`id`, `category`.`name`, `category`.`parent`, `category`.`created_at`, `category`.`updated_at` FROM `category` WHERE (`category`.`parent` IN (1, 2))
The issue I believe is when it's trying to attach the relationship query result to the base (parent) query result struct, it's not handling the pointer properly. I'm not sure if doing a comparison between a memory address and the value or there is some other issue somewhere where it should get de-referenced but its not.
Has anyone found a solution? I have the same problem :/
This may be a similar to this issue but when I try to do a has-many relationship that references the same model, it won't work if the parent field is a pointer (edit: used to say non-pointer but should have said pointer).
This is the non-working code:
The above code will return the parent categories without the children. If I check the logs, I see this error:
If I replace the ParentID with a non-pointer uint and run the code, it works as expected with the parents at the top level and the nested AllChildren.
Am I missing something?
I tried the duplicate struct that the above issue (they did Message and MessageX) and could get it to work that way but that's not really ideal.