postgresql - 时间被存储在没有时区的 postgres 数据库中,即使我提供它们来 Sequelize

标签 postgresql sequelize.js

我正在使用 sequelize 在我的 postgres 数据库中填充一个表。我有一个时间数据类型的字段,但时区没有被存储。我正在做一个像这样的批量创建:

await models.Event.bulkCreate(events);

其中事件是我添加的 JSON 格式的新实例。
以下是其中一个实例的示例:
    {
        "_REMOVE_INFO": "App Development - Alessandro Ferrari",
        "City": "Milano",
        "Region": "MI",
        "Country": "Italy",
        "Address": "Via Massimo D'Azeglio, 3, 20154",
        "Date": "2020-11-21",
        "Time": "9:00 AM PST",
        "Title": "Code your idea ",
        "Pic": "https://images.pexels.com/photos/546819/pexels-photo-546819.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
        "Description": "You'll learn the basic building blocks of a website and how to style them. No previous coding experience needed.You will leave with the core concepts of HTML (tags, elements, and attributes), and the basics of CSS (selectors, properties, and values) and some Javascript. All your work will be in a platform that will allow you to code along with your instructor, and continue to work on it after you leave!",
        "MaxCapacity": 20,
        "Type": "Workshop"
    }

但是在我运行脚本之后,postgres 没有存储数据的时区部分。事实上,它明确表示“没有时区的时间”,如下图所示:

enter image description here

在我的事件模型架构中,我将时间定义如下
    Time: {
        type: DataTypes.TIME,
        allowNull: false,
    }

我的问题是我格式化 JSON 的方式还是我的 Sequelize 配置?我该如何解决?

最佳答案

time without time zone is a Postgres data type 。如果要存储 time with time zonetimetz,则必须将列更改为该数据类型。
但是不要。如果您需要使用时区存储时间,请使用 timestamp with time zonetimestamptz 。在 Sequelize 中,这是 .DATE
The Postgres docs explain why to avoid time with zone

Although the date type cannot have an associated time zone, the time type can. Time zones in the real world have little meaning unless associated with a date as well as a time, since the offset can vary through the year with daylight-saving time boundaries.

The default time zone is specified as a constant numeric offset from UTC. It is therefore impossible to adapt to daylight-saving time when doing date/time arithmetic across DST boundaries.

To address these difficulties, we recommend using date/time types that contain both date and time when using time zones. We do not recommend using the type time with time zone (though it is supported by PostgreSQL for legacy applications and for compliance with the SQL standard). PostgreSQL assumes your local time zone for any type containing only date or time.


例如,我所在的位置是 -0700。我想存储午餐时间是中午。使用 timetz ,午餐在 12:00:00-0700 。几个月后,夏令时将结束,我们将“退回”一小时到 -0800。但是午餐仍然在 12:00:00-0700 ,现在是本地时间下午 1 点。
相反,如果我将午餐时间存储为 time ,则无论时区如何, 12:00 都是正确的。

关于postgresql - 时间被存储在没有时区的 postgres 数据库中,即使我提供它们来 Sequelize ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61990776/

相关文章:

ruby-on-rails - 什么数据库用于 15M 记录和快速搜索索引

postgresql - 如何导入或同步数据到Neo4?

mysql - 如何在 Node.js 中使用 express-winston 将日志存储到 mysql db

sql-server - Sequelize findOne() 和 SQL Server 的问题

python - pg_dump : too many command-line arguments (first is "--host=localhost")

database - 带密码的 PostgreSQL 身份验证

python - SQLAlchemy 继承不起作用

mysql - Sequelize 1 :m association 上的重复外键

javascript - 如何在nodejs中使用sequelize设置模型验证?

node.js - Sequelize v6.2.0 : include. model.getTableName 不是函数