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

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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
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

Other popular topics 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
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
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

We're in Beta

About us Mission Statement