批量postgres插入,其中时间戳通过sequialize设置了null约束



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

我已经建立了模型,并允许Sequalize制作表格。我现在想使用这样的东西来移动数据。insert into archive (id,title) select id, title from old_archives;然而,由于Sequelize使用createdAt和updatedAt列,我得到了null错误。有办法绕过这个吗?

错误为

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

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

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

这将从旧表中选择数据,并将当前时间戳添加到每一行。当然,您也可以插入您想要的任何其他日期,而不是NOW()

最新更新