Issue whilst creating a form based on many to many relation

Hi,

I am trying to create a form in which a user can choose a product from a list, which at the end will subtract calories of a given product from users daily calorie pool.

I am getting this error: key :foods not found in: %Calorie.UsersFoods.UserFood{__meta__: #Ecto.Schema.Metadata<:built, "users_foods">, food: #Ecto.Association.NotLoaded<association :food is not loaded>, food_id: nil, id: nil, inserted_at: nil, updated_at: nil, user: #Ecto.Association.NotLoaded<association :user is not loaded>, user_id: nil}

What am I doing wrong? This is what I have up until now:

user.ex

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

  schema "users" do
    field :name, :string
    field :username, :string
    field :password, :string, virtual: true
    field :password_hash, :string
    many_to_many(
      :foods,
      Calorie.Products.Food,
      join_through: "users_foods",
      on_replace: :delete
    )

    timestamps()
  end

products/food.ex

defmodule Calorie.Products.Food do
  import Ecto.Query
  import Ecto
  use Ecto.Schema
  import Ecto.Changeset

  schema "foods" do
    field :calories, :integer
    field :name, :string
    many_to_many(
      :users,
      Calorie.Accounts.User,
      join_through: "users_foods",
      on_replace: :delete
    )

    timestamps()
  end

users_foods/user_food.ex

defmodule Calorie.UsersFoods.UserFood do
  use Ecto.Schema
  import Ecto.Changeset

  alias Calorie.Accounts.User
  alias Calorie.Products.Food

  schema "users_foods" do
    belongs_to(:user, User)
    belongs_to(:food, Food)

    timestamps()
  end

  @required_fields ~w(user_id food_id)a
  def changeset(users_foods, params \\ %{}) do
    users_foods
    |> cast(params, @required_fields)
    |> validate_required(@required_fields)
    |> foreign_key_constraint(:user_id)
    |> foreign_key_constraint(:food_id)
    |> unique_constraint([:users, :foods],
      name: :user_id_food_id_unique_index,
      message: "dupa123"
    )

  end
end

Users Food migration

defmodule Calorie.Repo.Migrations.CreateUsersFoods do
  use Ecto.Migration


  def change do
    create table(:users_foods) do
      add :user_id, references(:users)
      add :food_id, references(:foods)
      timestamps()
    end


    create(index(:users_foods, [:user_id]))
    create(index(:users_foods, [:food_id]))

    create unique_index(:users_foods, [:user_id, :food_id], name: :user_id_food_id_unique_index)
  end
end


The error says

:foods not found in: %Calorie.UsersFoods.UserFood

and indeed we see that :foods is a field inside User schema and not UserFood schema.

Thank you for your reply!

I see that it tells me to insert :foods field inside UserFood schema, however I am not sure where and why as most examples that I saw didn’t require such a doing (example: Many to many associations in Elixir and Phoenix - DEV Community ) . I will be grateful for explanation :slight_smile: