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

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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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