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
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
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
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
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
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
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
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
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
I would like to know what is the best IDE for elixir development?
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
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
- #forms
- #api
- #metaprogramming
- #hex
- #security









