postgresql 使用 Ecto 和 Elixir 的默认日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28506589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Default datetime with Ecto & Elixir
提问by MartinElvar
I've just started working Elixir & Phoenix today, i am trying to add Ecto as a mapper, but i'm having some trouble using time.
我今天刚刚开始使用 Elixir & Phoenix,我正在尝试将 Ecto 添加为映射器,但是我在使用时间时遇到了一些麻烦。
This is my model.
这是我的模型。
schema "users" do
field :name, :string
field :email, :string
field :created_at, :datetime, default: Ecto.DateTime.local
field :updated_at, :datetime, default: Ecto.DateTime.local
end
I'm trying to set the created_at and updated_at per default, but when i try to compile this, i get the following error.
我试图默认设置 created_at 和 updated_at,但是当我尝试编译它时,出现以下错误。
== Compilation error on file web/models/user.ex ==
** (ArgumentError) invalid default argument `%Ecto.DateTime{day: 13, hour: 19, min: 47, month: 2, sec: 12, year: 2015}` for `:datetime`
lib/ecto/schema.ex:687: Ecto.Schema.check_default!/2
lib/ecto/schema.ex:522: Ecto.Schema.__field__/4
web/models/board.ex:9: (module)
(stdlib) erl_eval.erl:657: :erl_eval.do_apply/6
There is not much help to get in the documentation, what would be the correct way to do this?
在文档中没有太多帮助,正确的方法是什么?
回答by misaelpc
Defaults fields names are :inserted_at
and :updated_at
but you can merge with your own field names, passing a keyword list
默认字段名称是 :inserted_at
,:updated_at
但您可以与自己的字段名称合并,传递关键字列表
schema "users" do
field :name, :string
field :email, :string
timestamps([{:inserted_at,:created_at}])
end
回答by whatyouhide
:datetime
is the native Postgres data type for, well a datetime; this data type maps to a two-elements Elixir tuple ({{yy, mm, dd}, {hh, mm, ss}}
). An %Ecto.DateTime{}
struct is not a two-elements tuple, hence the compilation error.
:datetime
是用于日期时间的本地 Postgres 数据类型;此数据类型映射到一个包含两个元素的 Elixir 元组 ( {{yy, mm, dd}, {hh, mm, ss}}
)。的%Ecto.DateTime{}
结构不是一个两个元素的元组,因此编译错误。
You may want to set the type of your fields to Ecto.DateTime
, it should all work seamlessly.
您可能希望将字段的类型设置为Ecto.DateTime
,它应该都能无缝地工作。
Hereis the relevant documentation about primitive types and non-primitive types.
这是关于原始类型和非原始类型的相关文档。
PSyou may also want to have a look at Ecto.Schema.timestamps/1
, which is macro that expands to basically what you wrote manually (it adds the created_at
and updated_at
fields and it let's you choose what type they should be, defaulting to Ecto.DateTime
):
PS,您可能还想看看Ecto.Schema.timestamps/1
,这是一个宏,基本上可以扩展到您手动编写的内容(它添加created_at
和updated_at
字段,让您选择它们应该是什么类型,默认为Ecto.DateTime
):
schema "users" do
field :name, :string
field :email, :string
timestamps
end
回答by G Amini
You could also consider having the default not be in the schema, but in the migration: "created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"
您还可以考虑让默认值不在架构中,而是在迁移中: "created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"