Bulk postgres insert where timestamp has null constraint set through sequalize

穿精又带淫゛_ 提交于 2019-12-24 14:06:12

问题


I am using Postgres and Sequelize with my node app.

I have set up the models and allows Sequalize to produce the tables. I now want to move the data across using something like this. insert into archive (id,title) select id, title from old_archives; However, I am getting null errors as Sequelize uses createdAt and updatedAt columns. Is there a way around this?

Error is

ERROR: null value in column "createdAt" violates not-null constraint DETAIL: Failing row contains (2, null, Dewerstone Rocks, null, null, null, null, null, null, null, null).


回答1:


Instead of altering the old table you could alter the select statement to generate the required date

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

This selects the data from the old table, plus adds the current timestamp to each row. Of course you could also insert any other date you want instead of NOW()



来源:https://stackoverflow.com/questions/34614132/bulk-postgres-insert-where-timestamp-has-null-constraint-set-through-sequalize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!