I tried doing the following:
add :balance, :decimal, default: 0.0
add :balance, :decimal, default: "0.0"
add :balance, :decimal, default: Decimal.new("0.0")
The first 2 don't work at all as newly-created records still return nil
if I don't explicitly pass a value.
The 3rd one returns this error:
** (ArgumentError) unknown default
#Decimal<0.0>
for type:decimal
. :default may be a string, number, boolean, list of strings, list of integers, map (when type is Map), or a fragment(...)
The only workaround I can think of right now is to use put_change/3:
def changeset(account, attrs) do
account
|> put_change(:balance, Decimal.new("0.0"))
end
However, I prefer to do this on the migration itself. How do I do it?
I asked this same question on Elixir Slack and I was told that the first one should work.
I checked again and the first one DOES work. It's just that Repo.insert()
returns the column as nil
, but it shows up as #Decimal<0E-10>
if I retrieve the record again, which means the default value does work.