Note that the schema for SPI type to SPI type has a large amount of variation from schema to schema thus its not ideal to have just one db table. But at the end of the day they represent the same construct in my system in that they are all of a type of “SPI”
defmodule SPI1 do
schema "spi_1s" do
field(:foo, :string)
field(:bar, :string)
field(:baz, :string)
belongs_to(:user, User)
end
end
defmodule SPI2 do
schema "spi_2s" do
field(:X, :string)
field(:Y, :string)
field(:Z, :string)
belongs_to(:user, User)
end
end
defmodule USER do
schema "users" do
# Q: Is there a polymorphic way to handle the has_many side of multiple types of spis?
# It would be ideal if I could query all spis (spi_1 and spi_2) in one list without rolling
# that logic by hand.
has_many(:spi_1s, SPI_1)
has_many(:spi_2s, SPI_2)
end
end
It would depend on how you write the query/view. If you use a UNION (or anything else, but I usually just use unions), you can add an extra field to each that would indicate the source of the row.