2016-03-02 127 views

回答

8

timestamps/1函数接受一个选项关键字列表,您可以使用它设置默认值。

def change do 
    alter table(:channels) do 
    timestamps default: "2016-01-01 00:00:01", null: false 
    end 
end 


UPDATE外生> = 2.1
你需要使用新的类型NaiveDateTime

def change do 
    alter table(:channels) do 
    timestamps default: ~N[2017-01-01 00:00:01], null: false 
    end 
end 

如果您有更多的怀疑看看的documentation

+0

外生2.0已取消这个选项:(可能最好的解决办法是手动添加两个字段。 –

+2

原来的解决方案刚刚在Ecto 3.2中工作:) ...''timestamps default:“2016-01-01 00:00:01”,null:false' - 这是原始未更新的答案。 –

0

我想你会得到这个当你尝试更新的记录,我能想到2个可能的解决方案,您既可以在你的餐桌触摸inserted_at列通过运行一个UPDATE查询或添加功能,以您的外生模型,像这样

def create_changeset(model, attrs) do 
    model 
    |> cast(attrs, @required_fields, @optional_fields) 
    |> update_inserted_at 
    end 

    defp update_inserted_at(changeset) do 
    # check if the updated field is null set a new date 
    end