为什么 postgres 中的时区感知字段时区是天真的
Why is a timezone aware field timezone naive in postgres
我正在向我的方案添加一个 utc 时区感知列。但是,在我的数据库中,列类型为 timestamp(0) with out zone。
我做错了什么?
我的方案如下所示:
defmodule Transaction do
use Ecto.Schema
import Ecto.Changeset
schema "transaction" do
field :platform_transaction_at, :utc_datetime
end
end
我的迁移文件看起来像
defmodule Transaction.Migrations.AddPlatformTransactionDatetime do
use Ecto.Migration
def change do
alter table(:transaction) do
add :platform_transaction_at, :utc_datetime
end
end
end
PostgreSQL 没有用于存储带时区的时间戳的数据类型。有关更多讨论,请参阅 this elixir forum thread。您可能会反对确实存在一种名为 "timestamp with timezone" 的类型,但该线程中的答案进一步解释了:
There is the confusingly named timestamp with timezone but all it does is convert your input timestamp to UTC and convert it back to whatever your DB connection’s timezone is when reading (so for most cases it’s useless). It does not even store the offset/timezone. So you need to do that yourself somehow.
Ecto 迁移中的 :utc_datetime
和 :naive_datetime
在 Postgres 中创建相同的类型:timestamp without time zone
http://www.creativedeletion.com/2019/06/17/utc-timestamps-in-ecto.html
好消息是,您已经通过在架构和迁移中指定 :utc_datetime
来正确存储时间戳,因为即使它变成了 timestamp without timezone
、"all timezone-aware dates and times are stored internally in UTC." https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-TIMEZONES
我正在向我的方案添加一个 utc 时区感知列。但是,在我的数据库中,列类型为 timestamp(0) with out zone。
我做错了什么? 我的方案如下所示:
defmodule Transaction do
use Ecto.Schema
import Ecto.Changeset
schema "transaction" do
field :platform_transaction_at, :utc_datetime
end
end
我的迁移文件看起来像
defmodule Transaction.Migrations.AddPlatformTransactionDatetime do
use Ecto.Migration
def change do
alter table(:transaction) do
add :platform_transaction_at, :utc_datetime
end
end
end
PostgreSQL 没有用于存储带时区的时间戳的数据类型。有关更多讨论,请参阅 this elixir forum thread。您可能会反对确实存在一种名为 "timestamp with timezone" 的类型,但该线程中的答案进一步解释了:
There is the confusingly named timestamp with timezone but all it does is convert your input timestamp to UTC and convert it back to whatever your DB connection’s timezone is when reading (so for most cases it’s useless). It does not even store the offset/timezone. So you need to do that yourself somehow.
Ecto 迁移中的 :utc_datetime
和 :naive_datetime
在 Postgres 中创建相同的类型:timestamp without time zone
http://www.creativedeletion.com/2019/06/17/utc-timestamps-in-ecto.html
好消息是,您已经通过在架构和迁移中指定 :utc_datetime
来正确存储时间戳,因为即使它变成了 timestamp without timezone
、"all timezone-aware dates and times are stored internally in UTC." https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-TIMEZONES