Use Schemas to Seed a Database
Explore how to seed a database using Ecto schemas in Elixir by inserting records with nested associations in a single operation. Learn to use Repo.insert_all and Repo.insert with structs to efficiently populate tables and handle deeply nested data. Understand how these techniques simplify creating multiple related records, making database setup scripts concise and effective.
We'll cover the following...
Insert records with insert_all
Once we’ve set up schemas for our tables, inserting new records, even records with nested associations, can be done very concisely. Recall how we inserted new artists record using Repo.insert_all::
With insert_all, we provide the table name and a list of fields containing the new record’s values. If we want the ID of the new record, we have to ask for it with the returning ...