As per your E-R diagram you don’t have a many-to-many relationship - those would be represented logically as a relationship with “crow’s feet” at both ends. Typically many-to-many is implemented via an associative entity (join table).
Ecto’s many_to_many
is designed to work with associative entities - in effect hiding them to get back to the logical view rather than exposing the physical view.
Granted superficially Assistant
and Provider
look like an associative entity but as they hold information beyond the User
and Organization
keys they act as a full blown entity.
What you seem to be modelling is:
- (The role of)
Assistant
is fulfilled by aUser
(Assistant
belongs_to aUser
) - (The role of)
Provider
is fulfilled by aUser
(Provider
belongs_toUser
) Assistant
serves anOrganization
(Assistant
belongs_toOrganization
)Provider
serves anOrganization
(Provider
belongs_toOrganization
)Service
is delivered by aProvider
(Service
belongs_toProvider
)
(See step 1 (and step 1 only) of CSDP)
belongs_to
, has_one
, has_many
and many_to_many
are simply terms of the Ecto DSL - it’s not meant to accurately describe your model in terms of your domain.
Given that Organization
treats Assistant
and Provider
as separate entities even though we could abstract them to a type of Role
uniting them as a polymorphic entity, particularly in a relational model, is pre-mature given that at this point in time there is simply not enough value gained from introducing the necessary complexity (speculative design). The only evidence we have is “some fields with the same name” - reuse on its own is rarely is a good motivation for polymorphism (or inheritance).