简体   繁体   English

Sequelize Insert Cascade hasMany

[英]Sequelize Insert Cascade hasMany

Here again now having problem to insert hasMany.现在再次有问题插入hasMany。 tables Bill > BillDetail, im using freezeTableName: true to keep table in singular way表格 Bill > BillDetail,我使用 freezeTableName: true 以单一方式保存表格

Relations from Bill Model比尔模型的关系

Bill.associate = (models) => {
  Bill.hasMany(models.BillDetail, { foreignKey: "idBill" });
};

This is the code to insert Bill and create BillDetail with Bill "id", but ignore BillDetails unless i add Bill.belongsTo BillDetail but i will insert just the first object of BillDetail这是插入 Bill 并使用 Bill "id" 创建 BillDetail 的代码,但忽略 BillDetails 除非我添加 Bill.belongsTo BillDetail 但我只会插入 BillDetail 的第一个对象

Bill.create({
    idClient: 1,
    description: "new project",
    BillDetail: [
      { idService: 1 }, 
      { idService: 2 }
    ],
  },
  { include: BillDetail });

im following the guide, but i don't know what im missing.我遵循指南,但我不知道我错过了什么。 https://sequelize.org/master/manual/creating-with-associations.html https://sequelize.org/master/manual/creating-with-associations.html

You indicated include model with single name but you need to indicate with plural:您表示include具有单一名称的模型,但您需要用复数表示:

Bill.create({
    idClient: 1,
    description: "new project",
    BillDetails: [
      { idService: 1 }, 
      { idService: 2 }
    ],
  },
  { include: BillDetail });

compare with the example from the docs:与文档中的示例进行比较:

Product.create({
  id: 1,
  title: 'Chair',
  tags: [ // pay attention there is **tags** and not **tag**
    { name: 'Alpha'},
    { name: 'Beta'}
  ]
}, {
  include: [ Tag ]
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM