Ecto and PostgreSQL

This page was updated 13 Dec - 2020

Ecto it the database wrapper for mainly Postgresql. If you come from Ruby on Rails it is used instead of ActiveRecord but it is not an ORM. Ecto is a library and domain-specific language to interact with your database. It has four main components that can be used together all by itself.


use Ecto.Schema

schema "products" do
  field :description, :string
  field :name, :string



import Ecto.Changeset

def changeset(product, attrs) do
  |> cast(attrs, [:name, :description])
  |> validate_required([:name, :description])



import Ecto.Query

def list_active_products do
    p in Product,
    where: == true,
    order_by: [desc:, desc: p.inserted_at],
    limit: 10
  |> Repo.all()

Ecto and PostgreSQL Tutorials

Below are the latest tutorial on the topic of Ecto and PostgreSQL

Published 03 Feb - 2020

Setup a has_many / belongs_to in Phoenix

Something I do in EVERY project is to setup some sort of relation between resources. And even though Phoenix comes with generators for migrations and CRUD opeartions, you still need to modify the code to suite for the relations you want. In this short tutorial, I will go through my usual steps regarding setting up a basic has_many / belongs_to - relation.

Continue Reading

Published 10 Feb - 2020

Add an use a JSONB field in Phoenix and Ecto

PostgreSQL has native support for objects stored as JSON as actually binary JSON (or JSONB). With JSONB format, you can add index do the column for faster and more efficient reads. Even though the format supports both storing arrays of maps and nested maps, I suggest making it simple and just use an unnested single map. The use case I am going for is have some sort of userentered product attributes that I later can search and filter on.

Continue Reading

Published 11 Feb - 2020

Add Tags with Ecto has_many, through in Phoenix - Tagging part 1

I want to add tags to products. And as usual there are a situation where a product can have many tags and a tag can belong to many products. However, in my opnion, the best option is to use a 'has_many :through' relationship. So for this, I need a table called tags and a join table called taggings. In this tutorial, I will go through how would do this.

Continue Reading

Published 15 Feb - 2020

Create ghost loading cards in Phoenix LiveView

Unless you already didn't know, when a LieView component is mounted on a page, it runs the mount/2 function twice. One when the page is rendered from the initial request, and one one when the socket is connected. So if you have data that is slow or resource heavy to load, you should consider to wait until the socket is connected so the intial load is not in vein. A popular approach is to use ghost cards. A ghost gard is a loading placeholder that looks similar to the content that you try to load in but without actual data. In this tutorial I will show you how

Continue Reading

Published 31 Aug - 2020

Fuzzy find with Ecto in Phoenix LiveView

Fuzzy find is both a simple and a complex thing. Even though though it's simple to implement, its hard to get right from a UX perspective. Luckily, I can help you get going with the technological implementation.

Continue Reading

Published 02 Sep - 2020

Table sorting with Ecto and LiveView

A very common or even mandatory feature in e-commerce stores is the ability to sort a list of products by attributes. This is easy enough and a good fit for Phoenix LiveView. In this tutorial, I will build on an existing page that displays a list of products and implement sorting on product name and prices.

Continue Reading

Published 01 Jun - 2021

How to use Phoenix forms for embedded schema and JSONB

In this tutorial, I want to show you how you can setup form that stores the data in partly the columns in a database but also some of the fields in JSONB with an embedded schema. The reason you might want to do this instead of saving all entries in separate columns is that you can add more fields without changing the database.

Continue Reading

Published 06 May

Teams Feature with Phx.Gen.Auth

A very common feature in web applications, especially SAAS applications are the concept of teams where a user can have and belong to multiple teams. In this tutorial I want go through how this can be setup in Phoenix and Ecto in combination with Phx gen Auth. The idea is that each user has their own personal account. The personal account is not accessible to other users. This ensures that the code works exactly the same regardless of whether we are adding resources to a private account or an account with users. Once a new user record is created successfully, an action will take place and the user will be granted a personal account. After that, users can create more accounts and invite others to them. Users can change which account they are viewing in the user interface.

Continue Reading