Tutorial

How to use Phoenix forms for embedded schema and JSONB

jsonb forms ecto

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.

STEP 1 - Generate a new column

In this example, I already have setup a crud interface for products in a table called shop_products. What I need to to is to add a JSONB coulmn to that table and I will give it the generic name data.

mix ecto.gen.migration add_data_column_to_products

The way to specify a JSONB column in ecto is to add the type :map . The default is an empty map and I dont want it to be blank.

defmodule Tutorial.Repo.Migrations.AddDataToProducts do
  use Ecto.Migration  def change do
    alter table(:shop_products) do
      add :data, :map, default: %{}, null: false
    end
  end
end

NOTE that I dont added an index because I dont need one in this tutorial.

With the migration file in place, I need to run the migrations by:

mix ecto.migrate

STEP 2 - Add a data schema

The data will be en embedded schema with its own changeset, and if needed, its own validation rules. In this example, I want to add size and colour as additional fields to the products.

# lib/tutorial/shop/data.ex
defmodule Tutorial.Shop.Data do
  use Ecto.Schema
  import Ecto.Changeset  @primary_key false
  embedded_schema do
    field :size, :string
    field :color, :string
  end  @doc false
  def changeset(option, attrs) do
    option
    |> cast(attrs, [:size, :color])
    |> validate_required([])
  end
end

In the product schema, I need to make 3 changes.

  1. alias the Data schema
  2. and an embedsone for the Data shema. Also add the onreplace: :update
  3. in the changeset, add the line |> cast_embed(:data)
# lib/tutorial/shop/product.ex
defmodule Tutorial.Shop.Product do
  use Ecto.Schema
  import Ecto.Changeset  alias Tutorial.Shop.Data # <-- ADD THIS LINE  @primary<i>key {:id, :binary</i>id, autogenerate: true}
  @foreign<i>key</i>type :binary_id
  schema "shop_products" do
    field :archived<i>at, :naive</i>datetime
    field :name, :string    embeds<i>one :data, Data, on</i>replace: :update # <-- ADD THIS LINE    timestamps()
  end  @doc false
  def changeset(product, attrs) do
    product
    |> cast(attrs, [:name, :archived_at])
    |> validate_required([:name])
    |> cast_embed(:data) # <-- ADD THIS LINE
  end
end

I can test this by putting the data as a key in the valet_attrs map. I want to test this in both create and update.

 elixir
# test/tutorial/shop_test.exs
test "create_product/1 with data attributes casts and updates embedded data" do
  valid_attrs = Map.put(@valid_attrs, :data, %{size: "L", color: "green"})  assert {:ok, %Product{} = product} = Shop.create<i>product(valid</i>attrs)
  assert product.data.size == "L"
  assert product.data.color == "green"  update_attrs = Map.put(@valid_attrs, :data, %{size: "M", color: "red"})
  assert {:ok, %Product{} = product} = Shop.update_product(product, update_attrs)
  assert product.data.size == "M"
  assert product.data.color == "red"
end

STEP 3 - Add embedded fields to the form

Since I have setup an embedsone, and that behaves as a relation, I can use fieldsfor to store the values in the data column.

When I start the app, and test the form, it looks just like the other fields. If the fields dont show up, it is most likely because the data field in the database is defaulted to NULL. Make sure that defaults to an empty map.

When that is saved, I can test this in the console by getting the last product and see that the data is there.

Shop.get_product! "3ab9f40e-b290-4a58-9311-c0d88a79eaf1"
=> %Tutorial.Shop.Product{
  __meta__: #Ecto.Schema.Metadata<:loaded, "shop_products">,
  archived_at: nil,
  data: %Tutorial.Shop.Data{color: "Black", size: "L"},
  id: "3ab9f40e-b290-4a58-9311-c0d88a79eaf1",
  inserted_at: ~N[2021-06-01 08:19:21],
  name: "Test",
  updated_at: ~N[2021-06-01 11:13:56]
}

Related Tutorials

Published 10 Feb - 2020
Updated 01 May - 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 ..

Published 20 May - 2023

Handling Amount Fields in a Phoenix Application with Ecto Custom Types

In this tutorial, we will discuss a common problem faced in Elixir applications related to handling and storing formatted amount fields in a databas..