Creating Schemas

Let’s get our feet wet by looking at the DSL Ecto provides to create schemas. We’ll use the DSL to specify the fields we want to map, and their data types.

Mapping Schema Fields

Let’s start with our tracks table. We’ll have just five fields for now:

  • id: A unique ID for our track
  • title: A string representing the track’s title
  • duration: The length of the track in seconds
  • index: A number representing the track’s position in an album
  • number_of_plays: A counter that we increment every time we play the track

If you were writing an Elixir app that didn’t use Ecto, you would most likely create a %Track{} struct to hold this data. You’d open a new file named track.ex and add something like this:

 

Get Programming Ecto now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.