Ash FrameworkAF
Ash Framework3y ago
8 replies
WIGGLES

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
Was this page helpful?