Inverse polymorphism In ECTO

Hello Elixir Forum,

I wanted to ask for advice on how to represent an inverse polymorphic relationship using Ecto. Given my use case, I moved forward to building the relationship using a join table (single table inheritance and abstract tables are not really my use case).

I have Organizations Integrations and ProviderAConfiguration and ProviderBConfiguration , C, D, E and so on

Each Organization has many Integrations

Integrations is just a join table that includes organization_id, type, configuration_id

The need for a join table is for me very important for being able to easily list all the Integrations into an Organization.

Is there any idiomatic way to build this using Ecto helpers without having to build custom logic?
And, when I’m going to create the integrations migration I imagine I cannot use the references helper since configuration_id will no belong to a specific table.

def change do
    create table(:integrations) do
      add :type, :string
      add :organization_id, references(:organizations)
      add :configuration_id


To summarize. I need to create a “many to many” relationship where one of the foreign keys in the join table is defined by the type column.

Thank you in advance


Another acceptable solution would be:

Organization has_many Integrations
Integration has_one configuration based on the type.

In the integration table you may also consider using a polymorphic embed (jsonb).

1 Like

Thank you @mathieuprog after talking with @dlively I’ll probably move through a more simple Jsonb implementation

1 Like