簡體   English   中英

Sequelize Insert Cascade hasMany

[英]Sequelize Insert Cascade hasMany

現在再次有問題插入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 ]
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM