序列化bulkCreate()返回主键的NULL值 [英] Sequelize bulkCreate() returns NULL value for primary key
本文介绍了序列化bulkCreate()返回主键的NULL值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用节点编写其余部分,将其序列化为mySQL的ORM. 我正在使用bulkCreate函数批量创建记录.但作为回应,它返回 null 作为主键值.
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.
型号
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})
这篇关于序列化bulkCreate()返回主键的NULL值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文