Unclear on the syntax to seed data

I want to create a basic JSON API and need to seed data.

I ran the following code to create the schema

mix phx.gen.json Api Post posts title:string body:text

I added this line to the controller:

resources "/posts", PostController, except: [:new, :edit]

  scope "/api", HelloWeb do
    pipe_through :api
    resources "/posts", PostController, except: [:new, :edit]

I ran:

mix ecto.migrate

To seed data I am unsure of the syntax. I wrote the following in /priv

alias Hello.{Repo, Post}
Repo.insert!(%Post{title: "My first post", body: "It works!"})
Repo.insert!(%Post{title: "My second post", body: "It really does work."})

I ran mix ecto.migrate again.

My database doesn’t have any data in it. I figure I wrote the seed data incorrectly maybe.

Try mix run priv/repo/seeds.exs or mix ecto.setup as mix ecto.migrate only runs migrations.

Take a look at your mix.exs file, you’ll find an aliases/0 private function how you might want to handle seeding the database with Ecto.

  defp aliases do
      setup: ["deps.get", "ecto.setup"],
      "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],

For more about aliases, see the Testing with Ecto docs.

Is my initial Syntax correct?

Did you see any errors when running the seed file using mix run priv/repo/seeds.exs or mix ecto.setup?

Running mix ecto.migrate again will not run the code to seed data which would explain why your database does not have any data in it.

And on the offchance that’s not a typo, the mix run and default ecto.setup alias above expect the code to be in priv/repo/seeds.exs.

If you want to validate that your syntax is correct, try running your Phoenix app inside an IEx shell and then running those three lines.

You can also run your app inside IEx (Interactive Elixir) as:

    $ iex -S mix phx.server

source: Phoenix Up and Running Guide

My syntax was wrong. I fixed it. Thanks