umbra
Default :date in Migration
Simple question, how to add a default Ecto.Date value to a field without using timestamp?
alter table(:registro) do
modify :data, :date, [null: false, default: Ecto.Date.utc()]
end
I know that this isnt the right way to do, but the exception still bugs me
(ArgumentError) unknown default #Ecto.Date<2017-03-21> for type :date. :default may be a string, number, boolean, empty list or a fragment(…)
Thanks
Marked As Solved
outlog
I believe a fragment is best described as ‘custom sql’
the now() function is a function in postgres PostgreSQL: Documentation: 9.6: Date/Time Functions and Operators
modify :data, :date, [null: false, default: fragment("now()")]
should work, although you are using a date field, so I’m not 100% - you might need to use:
modify :data, :date, [null: false, default: fragment("current_date")]
Also Liked
umbra
Ok, can you explain whats a fragment and how to use it?
create table(:posts) do
add :inserted_at, :naive_datetime, default: fragment("now()")
end
the fragment() function is calling a now() function?
outlog
the exception message is actually pretty good.. you need to use a fragment - see docs for solution ![]()
umbra
Thank you very much! =)









