How do we seed data of a specific resource?

I want to add admin user by default through seeds
2 Replies
Stefan Wintermeyer
I like to use priv/repo/seeds.exs to seed data. Here's an example:
# Create levels
#
levels_attributes = [
%{id: 1, name: "A", position: 1},
%{id: 2, name: "B", position: 2},
%{id: 3, name: "C", position: 3}}
]

for level_attributes <- levels_attributes do
Feriendaten.Geo.Level
|> Ash.Changeset.for_create(:create, level_attributes)
|> Feriendaten.Geo.create!()
end
# Create levels
#
levels_attributes = [
%{id: 1, name: "A", position: 1},
%{id: 2, name: "B", position: 2},
%{id: 3, name: "C", position: 3}}
]

for level_attributes <- levels_attributes do
Feriendaten.Geo.Level
|> Ash.Changeset.for_create(:create, level_attributes)
|> Feriendaten.Geo.create!()
end
You can seed the data with the command mix run priv/repo/seeds.exs Sidenote: I always use mix ash_postgres.drop && mix ash_postgres.create && mix ash_postgres.migrate && mix run priv/repo/seeds.exs to get me a clean sheet during development.
talha-azeem
talha-azeemOP3y ago
Thank you

Did you find this page helpful?