Polymorphic Self References

Can't seem to figure out how to accomplish the following. I have a polymorphic table that is meant to model a tree relationship. this of course requires a belongs_to and has_many relationship to itself. How do I go about defining a self referential relationship that is mean to pickup on context provided by the consumer? Such that all the relationships are within the same table?
relationships do
belongs_to :parent, __MODULE__
has_many :children, __MODULE__, destination_attribute: :parent_id
end

postgres do
polymorphic? true

repo Butsby.Repo

references do
reference :parent, on_delete: :delete
end
end
relationships do
belongs_to :parent, __MODULE__
has_many :children, __MODULE__, destination_attribute: :parent_id
end

postgres do
polymorphic? true

repo Butsby.Repo

references do
reference :parent, on_delete: :delete
end
end
3 Replies
WIGGLES
WIGGLESOP2y ago
My guess is that this is where I want to reach for manual relationships? No I guess that doesn't work since the foreign key constraint will never be set. I guess I might not be able to use polymorphic resources for this.
ZachDaniel
ZachDaniel2y ago
Yeah, I haven’t considered this use case TBH. We could maybe work something up but I don’t think it's supported currently. You might need to define each resource manually.
WIGGLES
WIGGLESOP2y ago
I think all I really want to do is to pass on what the consumer specifies in the data_layer context but I guess I can write a macro module to generate the resources for me. In the meantime I'll probably write up a feature request on the github.

Did you find this page helpful?