# Default :date in Migration

**URL:** <https://forum.elixirforum.com/t/default-date-in-migration/4232>\
**Category:** Questions\
**Tags:** ecto, dates, how-to-question\
**Created:** [March 21, 2017, 4:52pm UTC](https://forum.elixirforum.com/t/default-date-in-migration/4232 "2017-03-21T16:52:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![umbra](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/u/ecc23a/32.png) [@umbra](https://forum.elixirforum.com/u/umbra)\
**Post date:** [March 21, 2017, 4:52pm UTC](https://forum.elixirforum.com/t/default-date-in-migration/4232/1 "2017-03-21T16:52:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![outlog](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@outlog](https://forum.elixirforum.com/u/outlog)\
**Post date:** [March 21, 2017, 5:02pm UTC](https://forum.elixirforum.com/t/default-date-in-migration/4232/2 "2017-03-21T17:02:15Z")

</div>

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

[https://hexdocs.pm/ecto/Ecto.Migration.html#fragment/1](https://hexdocs.pm/ecto/Ecto.Migration.html#fragment/1)

---

<div class="post-metadata">

**Author:** ![umbra](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/u/ecc23a/32.png) [@umbra](https://forum.elixirforum.com/u/umbra)\
**Post date:** [March 21, 2017, 5:14pm UTC](https://forum.elixirforum.com/t/default-date-in-migration/4232/3 "2017-03-21T17:14:21Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![outlog](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@outlog](https://forum.elixirforum.com/u/outlog)\
**Post date:** [March 21, 2017, 5:27pm UTC](https://forum.elixirforum.com/t/default-date-in-migration/4232/4 "2017-03-21T17:27:52Z")

</div>

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](https://www.postgresql.org/docs/9.6/static/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT)

`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")]`

---

<div class="post-metadata">

**Author:** ![umbra](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/u/ecc23a/32.png) [@umbra](https://forum.elixirforum.com/u/umbra)\
**Post date:** [March 21, 2017, 5:41pm UTC](https://forum.elixirforum.com/t/default-date-in-migration/4232/5 "2017-03-21T17:41:26Z")

</div>

Thank you very much! =)
