Ecto question (foreign key name & preload/association name)

Hi, please let me know if this is listed plainly in the docs and I skipped it.

I’m writing an app with Phoenix and Ecto, standard fare. I have Users and Rooms. Each Room belongs to a User, associated by a foreign key field called owner_id on the Rooms table. The schemas are like this:

defmodule MyApp.Rooms.Room do
  use Ecto.Schema
  import Ecto.Changeset

  schema "rooms" do
    field :name, :string
    belongs_to :user, SimpleVote.Accounts.User, foreign_key: :owner_id

    timestamps()
  end
end

defmodule SimpleVote.Accounts.User do
  use Ecto.Schema
  import Ecto.Changeset

  @derive {Inspect, except: [:password]}
  schema "users" do
    field :email, :string
    field :password, :string, virtual: true
    field :hashed_password, :string
    field :confirmed_at, :naive_datetime
    has_many :rooms, SimpleVote.Rooms.Room, foreign_key: :owner_id

    timestamps()
  end
end

I’m running into trouble with that Room.owner_id field. It looks like the struct turns preloads into the actual model (or table? name:


%Rooms.Room{
  __meta__: #Ecto.Schema.Metadata<:built, "rooms">,
  id: nil,
  inserted_at: nil,
  name: "room name",
  owner_id: 1364,
  updated_at: nil,
  user: #Ecto.Association.NotLoaded<association :user is not loaded>
}

Note the user key.

Assuming this question makes sense… how do I set the name of the association field? Instead of Room.user, I want Room.owner.

Thanks in advance.

1 Like

I figured it out. I need to belongs_to :owner. The first argument is the field name, not the name of the db table.

Embarrassed that this took me a few hours of searching and experimenting. This link helped me.

1 Like