如何在迁移模式下在Ecto模式中设置`DateTime`和带时区的`timestamp`(timestamptz`)PostgreSQL类型?

tor*_*tte 2 postgresql elixir ecto

想要DateTime在Ecto模式和迁移中使用,而不是默认值NaiveDateTime,也要timestamptz在PostgreSQL中使用,而不是默认值timestamp(aka。timestamp without time zone)。

tor*_*tte 5

ECTO迁移:切换至timestamptz:utc_datetime

注意Ecto.Migration.timestamps / 1)全局配置始终可以在本地覆盖。

1.全局配置

使用docs中:migration_timestamps配置选项:Ecto.Migration

# in ./config/dev.exs (for example)

config :app, App.Repo, migration_timestamps: [type: :timestamptz]
Run Code Online (Sandbox Code Playgroud)

并且可以Ecto.Migration.timestamps/1像往常一样在迁移中使用:

# ./priv/repo/migrations/20190718195828_create_users.exs

create table(:users) do
  add :username, :string, null: false

  timestamps()
end
Run Code Online (Sandbox Code Playgroud)

Postgres 适配器将自动切换灵药表示,以 DateTimeNaiveDateTime

2.本地配置

使用Ecto.Migration.timestamps / 1:type选项:

defmodule App.Repo.Migrations.CreateUsers do

  use Ecto.Migration

  def change do
    create table(:users) do
      add :username, :string, null: false

      timestamps(type: :timestamptz)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

ECTO SCHEMAS:切换至 :utc_datetime

1.全局配置

Ecto模式也需要进行修改以使用 :utc_datetime,否则NaiveDateTime默认情况下会期望它们 。稍微修改Ecto.Schemadocs中的示例 :

# Define a module to be used as base
defmodule MyApp.Schema do
  defmacro __using__(_) do
    quote do
      use Ecto.Schema

      # In case one uses UUIDs
      @primary_key {:id, :binary_id, autogenerate: true}
      @foreign_key_type :binary_id

      # ------------------------------------
      @timestamps_opts [type: :utc_datetime]

    end
  end
end

# Now use MyApp.Schema to define new schemas
defmodule MyApp.Comment do
  use MyApp.Schema

  schema "comments" do
    belongs_to :post, MyApp.Post

    timestamps()
  end
end
Run Code Online (Sandbox Code Playgroud)

2.本地配置

defmodule ANV.Accounts.User do

  use Ecto.Schema

  # -- EITHER --------------------------
  @timestamps_opts [type: :utc_datetime]

  schema "users" do

    field :username, :string

    # -- OR -----------------------
    timestamps(type: :utc_datetime)
  end
Run Code Online (Sandbox Code Playgroud)

资源资源


  • 劳/ tzdata

    DateTime 在Elixir中,“ 只能处理“ Etc / UTC”日期时间 ”,但可以使用自定义的时区数据库进行配置,这就是该 tzdata


+----------------------+------------------+------------------------+------------------------------+-----------------------------------+
|    Ecto 3 type       |    Elixir type   | Supports microseconds? | Supports DateTime functions? | Supports NaiveDateTime functions? |
+----------------------+------------------+------------------------+------------------------------+-----------------------------------+
| :utc_datetime_usec   | DateTime         |    YES                 |   YES                        |   YES                             |
| :utc_datetime        | DateTime         |    NO                  |   YES                        |   YES                             |
| :naive_datetime_usec | NaiveDateTime    |    YES                 |   NO                         |   YES                             |
| :naive_datetime      | NaiveDateTime    |    NO                  |   NO                         |   YES                             |
+----------------------+------------------+------------------------+------------------------------+-----------------------------------+

Run Code Online (Sandbox Code Playgroud)