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

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement