umbra

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

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

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

outlog

the exception message is actually pretty good.. you need to use a fragment - see docs for solution :slight_smile:

https://hexdocs.pm/ecto/Ecto.Migration.html#fragment/1

umbra

umbra

Thank you very much! =)

Last Post!

umbra

umbra

Thank you very much! =)

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

We're in Beta

About us Mission Statement