I am new to Phoenix and haven’t really found a clear answer to this question, so here goes.
I have a has_many belongs to relationship as follows:
defmodule Grid.Devices.Switch do
use Ecto.Schema
import Ecto.Changeset
alias Grid.Devices.Switch
schema "devices_switches" do
field :asset_tag, :integer
field :location, :string
has_many :ports_switch_ports, Grid.Ports.SwitchPorts, on_delete: :delete_all
timestamps()
end
end
defmodule Grid.Ports.SwitchPorts do
use Ecto.Schema
import Ecto.Changeset
alias Grid.Ports.SwitchPorts
schema "ports_switch_ports" do
field :duplex, :string
field :mac, :string
belongs_to :devices_switch, Grid.Devices.Switch
end
end
I’m assuming that when I preload with:
def get_switch!(id), do: Repo.get!(Switch, id) |> Repo.preload(:ports_switch_ports)
It would look for a column called devices_switch_id in the ports_switch_ports table. However it was looking for just switch_id. I fixed it by specifying the foreign_key on the has_many side of the relationship but I was trying to figure out why it was looking for what I am assuming as the wrong column name.
Any guidance would be much appreciated.