Ecto with Phoenix in 4 Minutes.

Brooklin Myers
4 min readAug 16, 2021

Learn How To Handle Data Persistence and Validation in Phoenix with Ecto.

If you are not already familiar with setting up a Phoenix project, read the Phoenix Getting Started Guide and Phoenix Installation Guide. If you are a Windows user, you can read my article Elixir & Phoenix v1.5.9 Getting Started on Windows 10.

Ecto.Schema

A schema is a definition of how your program models your data. For example, you might have a user schema that validates that all users must have a name, an email, and a password.

An Ecto Schema is used to map and validate data from one source into an Elixir Struct. Structs are extensions built on top of maps that provide compile-time checks and default values.

Here’s an example of a Blog Post schema using Ecto.Schema:

You can create the same schema by making a new phoenix project through the mix terminal command.

mix phx.new projectname

Then create a post model using the mix phoenix generator:

mix phx.gen.html Blog Post posts title:string content:text.

the schema method comes from the Ecto.Schema module. It takes the schema name and defines fields for the schema. the field method takes in a field name and field data type.

Ecto.Repo

Ecto.Repo is a repository is a layer above your database. The Ecto.Repo acts as an adapter for the database. The repository layer handles all communication between your application and the database. By default Ecto.Repo comes with an adapter for PostgreSQL, which knows how to store data in the PostgreSQL database.

The Ecto.Repo provides methods like all to interact with the underlying repository and database.

Repo.all takes in a query. In this case, it takes in the Post schema, which is just a short syntax for:

from(p in Post) is a query to grab all of the posts from the database.

Ecto.Query

Ecto also handles building queries for your database. Queries are used to retrieve and manipulate data from a repository. The query is passed to Ecto.Repo methods to handle retrieving or storing data in the database.

from

The from method comes from the Ecto.Query module. It allows you to select a particular schema to query from the repository. In this case, the Post schema.

where

where allows you to filter in only objects that match a condition. For example, you might query for a specific post where the title is “specific title.”

select

select allows you to query for only specific values instead of the entire struct. For example, you might only care to get a list of post titles.

join

To query relationships on the schema, use join. For example, if you also want to get the comments that belong to the post.

Ecto supports two different syntaxes for creating queries. All the examples above use keywords query syntax. The alternative syntax is pipe-based syntax.

Pipe-based syntax

Final Thoughts

Ecto enables data validation and persistence in phoenix applications. You have learned how to model your data using Ecto.Schema, how to communicate with the database using Ecto.Repo, and how to build queries for the database using Ecto.Query.

Have questions or thoughts? You can chat with me on Twitter: https://twitter.com/BrooklinJMyers or comment below.

--

--

Brooklin Myers

Software Engineer. I create educational content focused on technology for mobile and web applications.