Help us understand the problem. What is going on with this article?

Ectoのinserted_at/updated_atが自動設定される仕組み

More than 3 years have passed since last update.

deps/ecto/lib/ecto/schema.ex

@doc """
Generates `:inserted_at` and `:updated_at` timestamp fields.

The fields generated by this macro will automatically be set to
the current time when inserting and updating values in a repository.

## Options

  * `:type` - the timestamps type, defaults to `:naive_datetime`.
  * `:usec` - sets whether microseconds are used in timestamps.
    Microseconds will be 0 if false. Defaults to true.
  * `:inserted_at` - the name of the column for insertion times or `false`
  * `:updated_at` - the name of the column for update times or `false`
  * `:autogenerate` - a module-function-args tuple used for generating
    both `inserted_at` and `updated_at` timestamps

All options can be pre-configured by setting `@timestamps_opts`.
"""
defmacro timestamps(opts \\ []) do
  quote bind_quoted: binding() do
    timestamps =
      [inserted_at: :inserted_at, updated_at: :updated_at,
       type: :naive_datetime, usec: true]
      |> Keyword.merge(@timestamps_opts)
      |> Keyword.merge(opts)

    type      = Keyword.fetch!(timestamps, :type)
    precision = if Keyword.fetch!(timestamps, :usec), do: :microseconds, else: :seconds
    autogen   = timestamps[:autogenerate] || {Ecto.Schema, :__timestamps__, [type, precision]}

    if inserted_at = Keyword.fetch!(timestamps, :inserted_at) do
      Ecto.Schema.field(inserted_at, type, [])
      Module.put_attribute(__MODULE__, :ecto_autogenerate, {inserted_at, autogen})
    end

    if updated_at = Keyword.fetch!(timestamps, :updated_at) do
      Ecto.Schema.field(updated_at, type, [])
      Module.put_attribute(__MODULE__, :ecto_autogenerate, {updated_at, autogen})
      Module.put_attribute(__MODULE__, :ecto_autoupdate, {updated_at, autogen})
    end
  end
end
defmodule MyApp.MyModel do
  use MyApp.Web, :model

  schema "my_model" do
    ...
    timestamps
    ...

■ デフォルトのタイムスタンプ生成

  => Ecto.Schema.__timestamps__(:naive_datetime, :microseconds)

■ type,usecを指定した場合

  timestamps(type: Ecto.DateTime, usec: false)
  => Ecto.Schema.__timestamps__(DateTime, :seconds)

■ func指定

  timestamps(type: Ecto.DateTime, autogenerate: {Ecto.DateTime, :from_erl, [{{2000, 1, 1}, {0, 0, 0}}]})
  => Ecto.DateTime.from_erl({{2000, 1, 1}, {0, 0, 0}})
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away