Sequelize bulkCreate() returns NULL value for primary key

匿名 (未验证) 提交于 2019-12-03 03:06:01

问题:

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 primary key value.

Model

sequelize.define('category', {     cat_id:{         type:DataTypes.INTEGER,         field:'cat_id',         primaryKey: true,         autoIncrement: true,         unique:true     },     cat_name:{         type: DataTypes.STRING,         field: 'cat_name',         defaultValue:null     } }); 

Bulk Create operation :

var data = [         {             'cat_name':'fashion'         },         {             'cat_name':'food'         }     ];      orm.models.category.bulkCreate(data)     .then(function(response){         res.json(response);     })     .catch(function(error){         res.json(error);     }) 

response :

[   {     "cat_id": null,     "cat_name": "fashion",     "created_at": "2016-01-29T07:39:50.000Z",     "updated_at": "2016-01-29T07:39:50.000Z"   },   {     "cat_id": null,     "cat_name": "food",     "created_at": "2016-01-29T07:39:50.000Z",     "updated_at": "2016-01-29T07:39:50.000Z"   } ] 

回答1:

Tested in MySQL:

Model.bulkCreate(values, { individualHooks: true })



回答2:

You should set the returning option:

Model.bulkCreate(values, {returning: true}) 


回答3:

var data = [     {         'cat_name':'fashion'     },     {         'cat_name':'food'     } ];  Model.bulkCreate(data) .then(function() {   //(if you try to immediately return the Model after bulkCreate, the ids may all show up as 'null')   return Model.findAll() }) .then(function(response){     res.json(response); }) .catch(function(error){     res.json(error); }) 


回答4:

The success handler is passed an array of instances, but please notice that these may not completely represent the state of the rows in the DB. This is because MySQL and SQLite do not make it easy to obtain back automatically generated IDs and other default values in a way that can be mapped to multiple records. To obtain Instances for the newly created values, you will need to query for them again. http://docs.sequelizejs.com/en/latest/api/model/#bulkcreaterecords-options-promisearrayinstance



回答5:

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);  }); 


回答6:

From the documentation: please notice that these may not completely represent the state of the rows in the DB. This is because MySQL and SQLite do not make it easy to obtain back automatically generated IDs and other default values in a way that can be mapped to multiple records. To obtain Instances for the newly created values, you will need to query for them again. http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-bulkCreate

But you can pass an option to make it return the id

orm.models.category.bulkCreate(data, { returning: true }) 


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