Sequelize bulkCreate() returns NULL value for primary key

前端 未结 7 1682
孤城傲影
孤城傲影 2020-12-15 04:46

I am writing rest using node, sequelize as ORM for mySQL. I am using bulkCreate function to create record in bulk. But in response it is returning null for

7条回答
  •  情书的邮戳
    2020-12-15 05:15

    Unfortunately that doesn't work in the latest version. They explain why here: http://sequelize.readthedocs.org/en/latest/api/model/#bulkcreaterecords-options-promisearrayinstance

    note that the description specifically mentions mysql. You'll have to query for them. (example includes var _ = require('lodash');

    var cat = rm.models.category;
    cat.bulkCreate(data)
     .then(function (instances) {
        var names = _.map(instances, function (inst) {
          return inst.cat_name;
        });
        return cat.findAll({where: {cat_name: {$in: names}}); 
     })
     .then(function(response){
        res.json(response);
     })
     .catch(function(error){
        res.json(error);
     });
    

提交回复
热议问题