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

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
lanycrost
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

We're in Beta

About us Mission Statement