现在再次有问题插入hasMany。
表格 Bill > BillDetail,我使用 freezeTableName: true 以单一方式保存表格
比尔模型的关系
Bill.associate = (models) => {
Bill.hasMany(models.BillDetail, { foreignKey: "idBill" });
};
这是插入 Bill 并使用 Bill "id"创建 BillDetail 的代码,但忽略 BillDetails 除非我添加 Bill.belongsTo BillDetail 但我只会插入 BillDetail 的第一个对象Bill.create({
idClient: 1,
description: "new project",
BillDetail: [
{ idService: 1 },
{ idService: 2 }
],
},
{ include: BillDetail });
我正在遵循指南,但我不知道我错过了什么。https://sequelize.org/master/manual/creating-with-associations.html
最佳答案
您用单个名称表示 include
模型,但您需要用复数表示:
Bill.create({
idClient: 1,
description: "new project",
BillDetails: [
{ idService: 1 },
{ idService: 2 }
],
},
{ include: BillDetail });
与文档中的示例进行比较:Product.create({
id: 1,
title: 'Chair',
tags: [ // pay attention there is **tags** and not **tag**
{ name: 'Alpha'},
{ name: 'Beta'}
]
}, {
include: [ Tag ]
})
关于javascript - Sequelize Insert Cascade hasMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64841125/