Sequelize.js 引用错误列名的多对多关系的续集

Sequelize.js 引用错误列名的多对多关系的续集,sequelize.js,Sequelize.js,我有一个ReceiveCredit表,其中包含配方和成分表的主键,如下所示: 'use strict'; module.exports = { up: (queryInterface, Sequelize) => { return queryInterface.createTable('RecipeIngredients', { id: { allowNull: false, autoIncrement: true,

我有一个ReceiveCredit表,其中包含配方和成分表的主键,如下所示:

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('RecipeIngredients', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      recipe_id: {
        type: Sequelize.INTEGER, 
        references: { model: 'Recipes', field: 'id' }
      },
      ingredient_id: {
        type: Sequelize.INTEGER, 
        references: { model: 'Ingredients', field: 'id'}
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('RecipeIngredients');
  }
};
如果我只是尝试使用以下代码查询上表:

models.RecipeIngredient.findAll().then(all => console.log(all))
我得到以下错误:

Executing (default): SELECT "recipe_id", "ingredient_id", "createdAt", "updatedAt", "IngredientId", "RecipeId" FROM "RecipeIngredients" AS "RecipeIngredient";
Unhandled rejection SequelizeDatabaseError: column "IngredientId" does not exist
为什么Sequelize认为存在一个名为“IngreditId”的列?该列的名称为“成分id”

更新:增加模态定义

配方:

'use strict';
module.exports = (sequelize, DataTypes) => {
  var Recipe = sequelize.define('Recipe', {
    name: DataTypes.STRING
  }, {});
  Recipe.associate = function(models) {
    Recipe.belongsToMany(models.Ingredient, {
      as: 'ingredients', through: { model: models.RecipeIngredient, foreignKey: 'recipe_id'}
    })
    // associations can be defined here
  };
  return Recipe;
};
成分:

'use strict';
module.exports = (sequelize, DataTypes) => {
  var Ingredient = sequelize.define('Ingredient', {
    name: DataTypes.STRING
  }, {});
  Ingredient.associate = function(models) {
    Ingredient.belongsToMany(models.Recipe, { as: 'recipes', through: { model: models.RecipeIngredient, foreignKey: 'ingredient_id'}})
  };
  return Ingredient;
};
 'use strict';
module.exports = (sequelize, DataTypes) => {
  var RecipeIngredient = sequelize.define('RecipeIngredient', {
    recipe_id: DataTypes.INTEGER,
    ingredient_id: DataTypes.INTEGER
  }, {});
  RecipeIngredient.associate = function(models) {
    // associations can be defined here
  };
  return RecipeIngredient;
};
接受方:

'use strict';
module.exports = (sequelize, DataTypes) => {
  var Ingredient = sequelize.define('Ingredient', {
    name: DataTypes.STRING
  }, {});
  Ingredient.associate = function(models) {
    Ingredient.belongsToMany(models.Recipe, { as: 'recipes', through: { model: models.RecipeIngredient, foreignKey: 'ingredient_id'}})
  };
  return Ingredient;
};
 'use strict';
module.exports = (sequelize, DataTypes) => {
  var RecipeIngredient = sequelize.define('RecipeIngredient', {
    recipe_id: DataTypes.INTEGER,
    ingredient_id: DataTypes.INTEGER
  }, {});
  RecipeIngredient.associate = function(models) {
    // associations can be defined here
  };
  return RecipeIngredient;
};

在配置数据库的文件中,在其中放置一个define:

underscored: true,
underscoredAll: true,
freezeTableName: true,
my config.js的示例

require("dotenv").config();

module.exports = {
    dialect: process.env.DB_DIALECT,
    host: process.env.DB_HOST,
    username: process.env.DB_USERNAME,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_DATABASE,
    define: {
        timestamps: true,
        underscored: true,
        underscoredAll: true,
        freezeTableName: true,
    },
};


展示模型definitions@Anatoly我已经更新了模型定义。也许你应该在以下关联中指出
otherKey
选项,以及你指出的
foreignKey
选项?但为什么它只会抱怨IngredenId。@约翰多在你的查询中(选择“配方id”、“配料id”、“createdAt”、“更新数据”,“IngRadientId”、“RecipeId”从“RecipeIndients”变为“RecipeIndient”;未处理的拒绝后续选项卡BaseError:列“IngRadientId”不存在)IngredientId在RecipeId之前被调用,这就是为什么它只抱怨IngredientId要做的其他事情,当您创建关联时,不需要在模型定义中创建字段