Sequelize bulkCreate() returns 主键的 NULL 值

Sequelize bulkCreate() returns NULL value for primary key

我正在使用节点编写 rest,sequelize 为 mySQL 的 ORM。 我正在使用 bulkCreate 函数来批量创建记录。但作为回应,它返回 null 作为主键值。

型号

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

批量创建操作:

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

回复:

[
  {
    "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"
  }
]

您应该设置 returning 选项:

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

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

不幸的是,这在最新版本中不起作用。他们在这里解释了原因: http://sequelize.readthedocs.org/en/latest/api/model/#bulkcreaterecords-options-promisearrayinstance

请注意,描述中特别提到了 mysql。您必须查询它们。 (示例包括 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);
 });

在 MySQL 中测试:

Model.bulkCreate(values, { individualHooks: true })
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);
})

来自文档:请注意,这些可能不完全代表数据库中行的状态。这是因为 MySQL 和 SQLite 不容易以可以映射到多个记录的方式获取自动生成的 ID 和其他默认值。要为新创建的值获取实例,您将需要再次查询它们。 http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-bulkCreate

但是你可以传递一个选项让它成为 return id

orm.models.category.bulkCreate(data, { returning: true })
var data = [{
   'cat_name':'fashion'
  },
  {
   'cat_name':'food'
  }
 ];

orm.models.category.bulkCreate(data,{individualHooks: true})
 .then(function(response){
   res.json(response);
 })
 .catch(function(error){
   res.json(error);
 });