【问题标题】:Bulk postgres insert where timestamp has null constraint set through sequalize批量 postgres 插入,其中时间戳通过 sequelize 设置了空约束
【发布时间】:2016-04-09 10:08:01
【问题描述】:

我在我的节点应用程序中使用 Postgres 和 Sequelize。

我已设置模型并允许 Sequalize 生成表格。我现在想使用这样的东西移动数据。 insert into archive (id,title) select id, title from old_archives; 但是,由于 Sequelize 使用 createdAt 和 updatedAt 列,我收到空错误。有没有办法解决这个问题?

错误是

错误:“createdAt”列中的空值违反非空约束 详细信息:失败行包含 (2, null, Dewerstone Rocks, null, null, null, null, null, null, null, null)。

【问题讨论】:

  • 更新:我现在已经作弊并在旧表中添加了虚拟日期,以便我可以复制它们。也许不是答案......

标签: node.js postgresql sequelize.js


【解决方案1】:

您可以更改 select 语句以生成所需的日期,而不是更改旧表

INSERT INTO archive (id, title, createdAt) SELECT id, title, NOW() FROM old_archives;

这会从旧表中选择数据,并将当前时间戳添加到每一行。当然你也可以插入任何你想要的日期来代替NOW()

【讨论】:

    猜你喜欢
    • 2017-05-24
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 2019-07-15
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    相关资源
    最近更新 更多