a8t

a8t

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.

Marked As Solved

a8t

a8t

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.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement