简体   繁体   中英

Sequelize Insert Cascade hasMany

Here again now having problem to insert hasMany. tables Bill > BillDetail, im using freezeTableName: true to keep table in singular way

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.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

You indicated include model with single name but you need to indicate with plural:

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 ]
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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