顺序创建并填充foreignKey

时间:2018-06-21 18:16:46

标签: mysql promise foreign-keys sequelize.js

编写一个食谱应用程序,并在MySQL中使用Sequelize。

有两个相关的表: Recipes 表和 RecipeAmounts 表,其外键为 RecipeId

根据用户输入,配方可以具有一种或多种成分。添加配方时,必须同时填写食谱 RecipeAmounts 表。

我已经用promise创建了以下函数,但是我不明白如何正确地在RecipeAmounts中填充 RecipeId 外键。它必须与食谱表中新添加的食谱的 id 相同。

exports.addRecipe = function (req, res) {

  var promises = [];

  promises.push(
    db.Recipe.create({
      RecipeName: req.body.RecipeName,
      RecipeDescription: req.body.RecipeDescription
    })
  )

  for (var i = 0; i < req.body.RecipeIngredients.length; i++) {
    promises.push(
      db.RecipeAmount.create({
        Amount: req.body.RecipeIngredients[i].AmountForSmall,
        Size: 'sm',
        Type: 'smoothie',
        // IngredientId: 10,
        // RecipeId: 1
      })
    );

    promises.push(
      db.RecipeAmount.create({
        Amount: req.body.RecipeIngredients[i].AmountForMedium,
        Size: 'md',
        Type: 'smoothie',
        // IngredientId: 10,
        // RecipeId: 1
      })
    );

    promises.push(
      db.RecipeAmount.create({
        Amount: req.body.RecipeIngredients[i].AmountForLarge,
        Size: 'lg',
        Type: 'smoothie',
        // IngredientId: 10,
        // RecipeId: 1
      })
    );
  }
  sequelize.Promise.all(promises).then(function() {
    console.log("all the files were created");
});

};

我只是出于测试目的尝试对 RecipeId 进行硬编码,但是当我这样做时,我得到了这个错误:

Unhandled rejection SequelizeForeignKeyConstraintError: Cannot add or update a child row: a foreign key constraint fails

1 个答案:

答案 0 :(得分:0)

使其适用于此:

exports.addRecipe = function (req, res) {
    db.Recipe.create({
      RecipeName: req.body.RecipeName,
      RecipeDescription: req.body.RecipeDescription
    }).then(function(newRecipe) {
        var promises = [];
        for (var i = 0; i < req.body.RecipeIngredients.length; i++) {
            var RecipeId = newRecipe.dataValues.id;
            console.log(newRecipe.dataValues.id);
            promises.push(
              db.RecipeAmount.create({
                Amount: req.body.RecipeIngredients[i].AmountForSmall,
                Size: 'sm',
                Type: 'smoothie',
                // IngredientId: 10,
                RecipeId: RecipeId
              })
            );

            promises.push(
              db.RecipeAmount.create({
                Amount: req.body.RecipeIngredients[i].AmountForMedium,
                Size: 'md',
                Type: 'smoothie',
                // IngredientId: 10,
                RecipeId: RecipeId
              })
            );

            promises.push(
              db.RecipeAmount.create({
                Amount: req.body.RecipeIngredients[i].AmountForLarge,
                Size: 'lg',
                Type: 'smoothie',
                // IngredientId: 10,
                RecipeId: RecipeId
              })
            );
          }

          sequelize.Promise.all(promises).then(function() {
            console.log('All done YAYYYYYYY!!!!!!!');
          });