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
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
Popular in Questions
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
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Hi everyone,
I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Other popular topics
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
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
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something…
Haskell reminds me of Java, and e...
New
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









