热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

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

如何解决《如何在迁移模式下在Ecto模式中设置`DateTime`和带时区的`timestamp`(timestamptz`)PostgreSQL类型?》经验,为你挑选了1个好方法。

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



1> toraritte..:

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]

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

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

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

  timestamps()
end

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

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

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

资源资源

Ecto中的:utc_datetime和:naive_datetime之间的差异


劳/ tzdata

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


PostgreSQL,Elixir和Phoenix中的时区以及 如何在Ecto中将时间戳设置为UTC日期时间

第一篇文章中的一个非常方便的表格:

+----------------------+------------------+------------------------+------------------------------+-----------------------------------+
|    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                             |
+----------------------+------------------+------------------------+------------------------------+-----------------------------------+


特定于PostgreSQL的讨论和建议

不要使用时间戳(无时区)(PostgreSQL Wiki)

PostgreSQL中带/不带时区的时间戳之间的差异

在Rails和PostgreSQL中完全忽略时区(可接受的答案)

8.5。日期/时间类型(PostgreSQL手册)

9.9.3。时区(PostgreSQL手册)


推荐阅读
author-avatar
手机用户2502932937
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有