Sequelize update with association

后端 未结 3 1893
有刺的猬
有刺的猬 2020-12-15 15:58

In sequelize it\'s possible to create a row and all it\'s association in one go like this:

return Product.create({
  title: \'Chair\',
  User: {
    first_na         


        
3条回答
  •  无人及你
    2020-12-15 16:53

    First you have to find model including sub model which you want to update. then you can get reference of sub model to update easily. i am posting an example for your reference. hope it will help.

    var updateProfile = { name: "name here" };
    var filter = {
      where: {
        id: parseInt(req.body.id)
      },
      include: [
        { model: Profile }
      ]
    };
    
    Product.findOne(filter).then(function (product) {
      if (product) {
        return product.Profile.updateAttributes(updateProfile).then(function (result) {
          return result;
        });
      } else {
        throw new Error("no such product type id exist to update");
      }
    });
    

提交回复
热议问题