jmurphyweb

jmurphyweb

:default option causes inconsistent behaviour for Ecto.Schema field

I find the :defualt option of Ecto.Schema’s :field function a bit confusing.

From my understanding, passing empty string "" to the cast function is usually equivalent to passing nil.

However, if there is a default: value set, it changes the behaviour and "" param is now treated differently from nil param.

This is demonstrated below:

defmodule A do
  use Ecto.Schema

  schema "a" do
    field(:a, :integer, default: 5)
    field(:b, :integer)
  end
end
iex(1)> changeset = Ecto.Changeset.cast(%A{},  %{"a" => "", "b" => ""}, [:a, :b])
#Ecto.Changeset<action: nil, changes: %{}, errors: [], data: #A<>, valid?: true>
iex(2)> Ecto.Changeset.fetch_field!(changeset, :a) 
# 5
iex(3)> Ecto.Changeset.cast(%A{a: 1, b: 1},  %{"a" => "", "b" => ""}, [:a, :b])
#Ecto.Changeset<
  action: nil,
  changes: %{a: 5, b: nil},
  errors: [],
  data: #A<>,
  valid?: true
>
iex(4)> Ecto.Changeset.cast(%A{a: 1, b: 1},  %{"a" => nil, "b" => nil}, [:a, :b])
#Ecto.Changeset<
  action: nil,
  changes: %{a: nil, b: nil},
  errors: [],
  data: #A<>,
  valid?: true
>

I guess I found this surprising so wanted to ask why it’s the case or if it is documented anywhere?

Marked As Solved

hauleth

hauleth

Yes, this is documented in Ecto.Changeset.cast/3 in the option :empty_values. You can force the same behaviour by using

changeset = Ecto.Changeset.cast(%A{}, %{"a" => "", "b" => ""}, [:a, :b], empty_values: [])

:default just treats the values literally.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
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

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement