我正在使用 sequelize 将数据批量插入到两个彼此具有一对一关联的表中。 但是我在使用 bulkInsert 时遇到问题,因为批量插入不返回任何内容。如文档中所述:
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
})
我有一个 UserDetail 表,如何添加 User.hasOne(UserDetail) 映射?
最佳答案
您应该能够在您的 userDetail 模型定义中定义它
module.exports = (sequelize, DataTypes) => {
var UserDetail = sequelize.define("user_detail", {
//insert fields here
});
UserDetail.belongsTo(models.User, {
foreignKey: {
allowNull: false
}
});
return UserDetail;
};
确保用户模型定义中有外键
关于javascript - 使用 hasOne 关联 Sequelize Bulk Insert。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35683160/