I have this schema:
schema "editables" do
field :title, :string
field :content, :binary
timestamps
end
I would like that on application start-up a few rows were automatically created and populated, say I want to create 6 entries with the :title
field containing: page1, page2,...
I should I do it?
My suggestion: create an script file that will populate the database. Let's call it priv/repo/seeds.exs
:
alias MyApp.Repo
Repo.insert! %MyApp.Data{...}
Repo.insert! %MyApp.Data{...}
In development you can run it as
mix run priv/repo/seeds.exs
or when you need in production:
MIX_ENV=prod mix run priv/repo/seeds.exs
I can't see any reason for you to do this every time the app starts. Imagine every command you run in development, test or production now needs to pay the penalty of creating data in the database. It is not a good idea.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With