Sequelize如何插入更新多行?

时间:2016-08-19 09:43:43

标签: node.js postgresql sequelize.js

我尝试使用Sequelize在我的数据库中插入多行但由于某些约束违规而导致for循环卡住。

        let dataset = [
             ["2016-01-01","987"],
             ["2016-01-02","987"],
             ["2016-01-03","987"],
             ["2016-01-04","987"],
             ["2016-01-05","987"],
             ["2016-01-06","987"],
             ["2016-01-07","987"],
             ...
        ];
        for(let row of dataset) {       
            model.findOrCreate({
                where: {date_prc: row[0]},
                defaults: {value: row[1]}
            });
        }

当我查看SQL日志时,它正在检查是否已插入日期,如果不是,则尝试插入该行。但在某些情况下,要插入缺少的日期行,它会插入下一个日期。

例如在我看到的日志中:

 select 2016-01-03 // this one is missing
 select 2016-01-04 // this one is not missing
 insert 2016-01-04 // it is inserting 2016-01-04 instead of 2016-01-03

0 个答案:

没有答案
相关问题