How to handle schemas polymorphism in Phoenix? : <p>The recommended way to handle polymorphic associations in Phoenix seems to be adding an intermediate schema that contains the references to the other schemas:</p>

<ul>
<li><a href="https://stackoverflow.com/questions/33184593/inverse-polymorphic-with-ecto">Inverse polymorphic with ecto</a></li>
<li><a href="https://hexdocs.pm/ecto/Ecto.Schema.html#belongs_to/3-polymorphic-associations" rel="noreferrer">https://hexdocs.pm/ecto/Ecto.Schema.html#belongs_to/3-polymorphic-associations</a>).</li>
</ul>

<p>So if I would like to create schemas with different kinds of animals, I would do:</p>

<pre><code>defmodule Animal do
  use Ecto.Model

  schema "animals" do
    belongs_to(:dog, Dog)
    belongs_to(:cat, Cat)
    belongs_to(:owner, PetOwner)
  end
end

defmodule Dog do
  use Ecto.Model

  schema "dogs" do
  end
end

defmodule Cat do
  use Ecto.Model

  schema "cats" do
  end
end

defmodule PetOwner do
  use Ecto.Model

  schema "pet_owners" do
    has_one(:pet, Animal)
  end
end
</code></pre>

<p>But I could also have the <code>PetOwner</code> schemas containing a binary field and the type:</p>

<pre><code>defmodule Dog do
  use Ecto.Model

  schema "dogs" do
  end
end

defmodule Cat do
  use Ecto.Model

  schema "cats" do
  end
end

defmodule PetOwner do
  use Ecto.Model

  schema "pet_owners" do
    field(:pet, :binary)
    field(:pet_type, :integer)
  end
end
</code></pre>

<p>Or even just having a nullable reference to all the animals in the owner schema:</p>

<pre><code>defmodule Dog do
  use Ecto.Model

  schema "dogs" do
    belongs_to(:owner, PetOwner)
  end
end

defmodule Cat do
  use Ecto.Model

  schema "cats" do
    belongs_to(:owner, PetOwner)
  end
end

defmodule PetOwner do
  use Ecto.Model

  schema "pet_owners" do
    has_one(:cat, Cat)
    has_one(:dog, Dog)
  end
end
</code></pre>

<p>The first method seems to add complexity to the schemas. What are the pros and cons of the different methods?</p>

<p>EDIT: Let's assume that a pet owner can own only one pet, if the schema allows multiple pet, the verification is done in the changeset.</p>
