Typography

活版印字


  • Home
  • Archive
  • Categories
  • Tags
  •  

© 2020 alincode

Theme Typography by Makito

Proudly published with Hexo

Sequelize 一對多更新

Posted at 2016-04-17 Sequelize One to Many 

記錄一下自己犯的錯,因為 promotion.setProducts 已經算是另一個更新程序,所以需要另外加 await。

更新 1:M 片段

PromotionService.js
1
2
3
4
5
6
7
8
let productIds = [1, 2];
let products = await db.Product.findAll({
where: {
id: productIds
}
});
await promotion.setProducts(products); // 很重要
promotion.save();

db schema 片段

PromotionService.js
1
2
3
4
5
associate: function(models) {
Promotion.belongsToMany(models.Product, {
through: 'PromotionProduct'
});
}

Share 

 Previous post: 好書推薦 - 如何閱讀一本書 Next post: Promise Chaining 

© 2020 alincode

Theme Typography by Makito

Proudly published with Hexo