silverdr

silverdr

Embedded schema and JSONB

This is an example from one of the very useful “FullstackPhoenix” tutorials, namely:

# 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

When saving the record, which embeds_one of the above, the whole “embedded_schema” gets stored, including fields, which are not set. This seems to make sense to me. At least as default behaviour. Out of curiosity – the question: is there a switch/option/other simple method, which would make it store only the fields, which are actually used/set?

Marked As Solved

evadne

evadne

You could write a custom Ecto type which only stores fields which are not nil. The wire protocol is forgiving. Your custom type can have a type of :map. In dump/1 you can emit a trimmed-down Map.

Last Post!

silverdr

silverdr

I don’t think it’s worth the effort in the case I have at hand. Was curious if somebody thought of such scenario and maybe provided an option OOB. But yes, if dumping whole maps becomes an issue then such custom type might help, TNX.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement