由于inserted_at
和updated_at
不能是null
,因此无法使用:
def change do
alter table(:channels) do
timestamps
end
end
** (Postgrex.Error) ERROR (not_null_violation): column "inserted_at" contains null values
有没有一种简单的方法可以在不复制
timestamps
'功能的情况下完成此任务?
最佳答案
timestamps/1
函数接受选项关键字列表,您可以使用它来设置默认值。
def change do
alter table(:channels) do
timestamps default: "2016-01-01 00:00:01", null: false
end
end
更新Ecto> = 2.1
您需要使用新的
NaiveDateTime
类型def change do
alter table(:channels) do
timestamps default: ~N[2017-01-01 00:00:01], null: false
end
end
如果您有更多疑问,请看看documentation
关于elixir - 如何使用Ecto的时间戳将时间戳添加到现有表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35744390/