Introduction to Schemas

Get a brief introduction to Elixir structs, schema modules, and associations.

We'll cover the following

Functional programs have a set of data structures that form the backbone of the codebase. We’ve got things like artists, albums, tracks, and so on in the music database. The Ecto.Schema module helps us map those data structures to database tables to seamlessly move data back and forth between our Elixir code and the database. We create the mappings with an elegant, concise DSL. Then, we use associations to connect related structures.

What will we do?

We’ll create more streamlined operations to query, insert, and update data with schemas. Schemas are pretty flexible and can be used to map data from any data source into our Elixir code. We’ll explore some of those use cases later in the course.

  • We’ll start by learning how to set up schemas and look at the data types that Ecto supports.
  • We’ll then see how to integrate our schemas into queries and convert some schema-less queries from the last chapter into leaner schema-based queries.
  • We’ll also look at instances when it’s best to avoid using schemas altogether.
  • We’ll look at how we can use associations to build relationships between database tables and integrate those associations into queries.
  • We’ll show how schemas simplify inserting new records, even records with complicated associations.

Get hands-on with 1400+ tech skills courses.