[英]Sequelize Bulk Insert with hasOne association.
I am using sequelize to bulk insert data into two tables which have one-to-one association with each other. 我正在使用sequelize将数据批量插入两个具有一对一关联的表中。 But I am having a problem with bulkInsert because bulk insert doesn't return anything.
但是我对bulkInsert遇到问题,因为批量插入不返回任何内容。 As stated in the docs:
如文档中所述:
User.bulkCreate([
{ username: 'barfooz', isAdmin: true },
{ username: 'foo', isAdmin: true },
{ username: 'bar', isAdmin: false }
]).then(function() { // Notice: There are no arguments here, as of right now you'll have to...
return User.findAll();
}).then(function(users) {
console.log(users) // ... in order to get the array of user objects
})
I have a UserDetail table, how can I add User.hasOne(UserDetail) mapping? 我有一个UserDetail表,如何添加User.hasOne(UserDetail)映射?
You should be able to define it in your userDetail model defintion 您应该能够在userDetail模型定义中定义它
module.exports = (sequelize, DataTypes) => {
var UserDetail = sequelize.define("user_detail", {
//insert fields here
});
UserDetail.belongsTo(models.User, {
foreignKey: {
allowNull: false
}
});
return UserDetail;
};
Make sure you have a foreign key in the User model defintion 确保用户模型定义中有外键
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.