Skip to content

Conversation

@somoza
Copy link
Contributor

@somoza somoza commented May 11, 2022

It's important to show on the @doc that this parameter can be sent too. One practical case is to set the timestamp with the default fragment now().

timestamps(default: fragment("NOW()"))

I have seen people doing this:

def change do
    create table(:activities) do
      add :name, :string , null: false
      timestamps()
    end

    alter table(:activities) do
      modify(:inserted_at, :timestamp, default: fragment("NOW()"))
      modify(:updated_at, :timestamp, default: fragment("NOW()"))
    end
end

Instead of this:

def change do
    create table(:activities) do
      add :name, :string , null: false
      timestamps(default: fragment("NOW()"))
    end
end

@josevalim josevalim merged commit 108d01b into elixir-ecto:master May 11, 2022
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@warmwaffles
Copy link
Member

Haha, I've been adding those fields by hand instead of using timestamps(), didn't know I could pass :default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants