Seed Data with Elixir

Hello,

I am working on a new application with Elixir, Dish_out. I want to see Data I follow this tutorial with Elixir Casts. However, I am not sure if what I am doing is correct because my seed file does not work.
My Seed File:

alias DishOut.Recipes

Recipes.insert!(
    %{title: "Mac N Cheese", ingredients: "Elbow Pasta, Milk, Cheese, Heavy cream", summary: "Bring pasta to a boil for 10 mins, drain water, add milk, cream, and cheese in. Stir and allow to cool before consuming.", date: "032022"}
)

In video the moderator alias another file, what is that file?

2 Likes

Corresponding tweet for this thread:

Share link for this tweet.

2 Likes

Looking at the code… there is no module called Recipes.

One is called Foods, but not Recipes.

Hmm? No error message?

You might test this in the console directly.

2 Likes

Yes, Recipes don’t exist. I thought we were creating a new one to seed the file?

What do I reference my Schema which is “Delish_foods”

so would it alias DishOut.Delish_foods?

sorry I am a beginner this si my first time seeding a file in elixir.

2 Likes

The github repo You are following looks like a beginner repo too.

The right name should be DishOut.Foods.Food

delish_foods is the name of the table.

PS. I did not watch the screencast, so I don’t know what’s in.

2 Likes

I see so I reference the whole file DishOut.Foods.Food not just a specific struct

2 Likes

My error after running mix run priv/repo/seed.exs

mix run priv/repo/seeds.exs
** (UndefinedFunctionError) function DishOut.Foods.Food.insert!/1 is undefined or private
    (dish_out 0.1.0) DishOut.Foods.Food.insert!(%{date: 5062002, ingredients: "Elbow Pasta, Milk, Cheese, Heavy cream", summary: "Bring pasta to a boil for 10 mins, drain water, add milk, cream, and cheese in. Stir and allow to cool before consuming.", title: "Mac N Cheese"})
    (elixir 1.12.0) lib/code.ex:1261: Code.require_file/2
    (mix 1.12.0) lib/mix/tasks/run.ex:146: Mix.Tasks.Run.run/5
    (mix 1.12.0) lib/mix/tasks/run.ex:86: Mix.Tasks.Run.run/1
    (mix 1.12.0) lib/mix/task.ex:394: anonymous fn/3 in Mix.Task.run_task/3
    (mix 1.12.0) lib/mix/cli.ex:84: Mix.CLI.run_task/2
    (elixir 1.12.0) lib/code.ex:1261: Code.require_file/2

is it because of the input of the data?

2 Likes

There is no insert! function in the Food structure. And Foods should be the module used to create Food struct.

You cannot invent function name, it needs to exists in the file.

  @doc """
  Creates a food.
  ## Examples
      iex> create_food(%{field: value})
      {:ok, %Food{}}
      iex> create_food(%{field: bad_value})
      {:error, %Ecto.Changeset{}}
  """
  def create_food(attrs \\ %{}) do
    %Food{}
    |> Food.changeset(attrs)
    |> Repo.insert()
  end

The closest is this one, and should be used like this…

DishOut.Foods.create_food(attrs)
3 Likes