john doe
john doe

Reputation: 9660

Sequelize Many to Many Relationship Referring to the Wrong Column Name

I have a RecipeIngredient table which contains the PK of Recipe and Ingredient tables as shown below:

'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');
  }
};

If I simply try to query the above table using the following code:

models.RecipeIngredient.findAll().then(all => console.log(all))

I get the following error:

Executing (default): SELECT "recipe_id", "ingredient_id", "createdAt", "updatedAt", "IngredientId", "RecipeId" FROM "RecipeIngredients" AS "RecipeIngredient";
Unhandled rejection SequelizeDatabaseError: column "IngredientId" does not exist

Why does Sequelize thinks that there is a column named "IngredientId"? The name of the column is "ingredient_id".

UPDATED: Modal Definitions Added

Recipe:

'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;
};

Ingredient:

'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;
};

RecipeIngredient:

 '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;
};

Upvotes: 0

Views: 451

Answers (1)

Bruno Santi
Bruno Santi

Reputation: 192

In the file that you configure the database, put a define with it inside:

underscored: true,
underscoredAll: true,
freezeTableName: true,

Example of 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,
    },
};

Upvotes: 1

Related Questions