【问题标题】:Sequelize bulkCreate() returns NULL value for primary keySequelize bulkCreate() 返回主键的 NULL 值
【发布时间】:2016-05-06 21:06:06
【问题描述】:

我正在使用 node 编写 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"
  }
]

【问题讨论】:

    标签: mysql node.js orm sequelize.js


    【解决方案1】:

    您应该设置returning 选项:

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

    【讨论】:

    • 但仅在 postgres 和 mssql 中
    • 它不会更新values 数组。它将返回一个新数组。
    【解决方案2】:

    在 MySQL 中测试:

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

    【讨论】:

    • { individualHooks: true } 具有极大的误导性,至少在 Sequelize 3 中是如此。我刚刚追踪它,它又恢复为进行个人保存!这不是散装的!很抱歉成为坏消息的承担者......
    • 这应该是公认的答案,因为他的问题与mysql有关。
    • 我在 MySQL 中测试过,目前不工作
    【解决方案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);
    })
    

    【讨论】:

    • 在您的示例中很好地使用了 cmets。你的第一个答案做得很好。欢迎(另一个女人,YAY)加入社区;)
    【解决方案4】:

    成功处理程序传递了一个实例数组,但请注意,这些可能不完全代表数据库中行的状态。这是因为 MySQL 和 SQLite 不容易以可以映射到多条记录的方式获取自动生成的 ID 和其他默认值。要获取新创建值的实例,您需要再次查询它们。 http://docs.sequelizejs.com/en/latest/api/model/#bulkcreaterecords-options-promisearrayinstance

    【讨论】:

      【解决方案5】:

      不幸的是,这在最新版本中不起作用。他们在这里解释了原因: 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);
       });
      

      【讨论】:

        【解决方案6】:

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

        但是你可以传递一个选项让它返回 id

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

        【讨论】:

          【解决方案7】:
          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);
           });
          

          【讨论】:

          • 虽然此代码 sn-p 可能是解决方案,但包含解释确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
          猜你喜欢
          • 2019-11-28
          • 2019-07-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-14
          • 1970-01-01
          • 2019-09-24
          • 1970-01-01
          相关资源
          最近更新 更多