[英]Sequelize Bulk Insert with hasOne association.
我正在使用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;
};
確保用戶模型定義中有外鍵
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.