: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?

1 Like

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.

1 Like