Sequelize - name: 'SequelizeDatabaseError', parent: Error: Unknown column 'Product.product_category_id' in 'field list'

Viewed 62

I'm having troubles with sequelize. I don't know why is making and putting more columns in the select section.

The problem seems to be only in the relation ProductCategory and Product models.

i tried everything to fix it, but i can't.

here's the code :

ProductCategory Model:

module.exports = (sequelize, dataTypes) => {
  let alias = 'ProductCategory';
  let cols = {
    id: {
      type: dataTypes.SMALLINT(3),
      notNull: true,
      primaryKey: true,
      autoIncrement: true,
    },
    name: {
      type: dataTypes.STRING(60),
      defaultValue: null,
    },
  };

  let config = {
    tableName: 'product_categories',
    timestamps: false,
    underscored: true,
  };

  const ProductCategory = sequelize.define(alias, cols, config);

  ProductCategory.associate = function (models) {
    ProductCategory.hasMany(models.Product, {
      foreingKey: 'category_id',
      as: 'ProductCategory',
    });
  };

  return ProductCategory;
};

Product Model:

module.exports = (sequelize, dataTypes) => {
  let alias = 'Product';
  let cols = {
    id: {
      type: dataTypes.INTEGER,
      primaryKey: true,
      notNull: true,
    },
    brand_id: {
      type: dataTypes.SMALLINT(8),
      defaultValue: null,
    },
    gender: {
      type: dataTypes.STRING(30),
      defaultValue: null,
    },
    discount_percentage: {
      type: dataTypes.SMALLINT(3),
      defaultValue: null,
    },
    price: {
      type: dataTypes.DECIMAL(11, 2),
      defaultValue: null,
    },
    description: {
      type: dataTypes.STRING(500),
      defaultValue: null,
    },
    color: {
      type: dataTypes.STRING(30),
      defaultValue: null,
    },
    category_id: {
      type: dataTypes.SMALLINT(3),
      defaultValue: null,
    },
  };

  let config = {
    tableName: 'products',
    timestamps: false,
    underscored: true,
  };

  const Product = sequelize.define(alias, cols, config);

  //++++++++++++++ Products associations ++++++++++++++++++++++++++
  Product.associate = function (models) {
    Product.belongsTo(models.Brand, {
      foreingKey: 'brand_id',
      as: 'Brand',
    });

    Product.belongsTo(models.ProductCategory, {
      foreingKey: 'category_id',
      as: 'ProductCategory',
    });

    /*Product.belongsTo(models.ProductImage,{
    
            foreingKey: "product_id"
        } )*/

    Product.belongsToMany(models.Cart, {
      through: 'cart_products',
      foreingKey: 'product_id',
      otherKey: 'cart_id',
      timestamps: false,
    });

    Product.belongsToMany(models.Size, {
      through: models.ProductSize,
      foreingKey: 'product_id',
      otherKey: 'size_id',
      timestamps: false,
    });
  };

  return Product;
};

Error:

Error_1

Error_2

thanks for the help

1 Answers

You misspelled foreignKey option in all association definitions:

ProductCategory.hasMany(models.Product, {
      // should be foreignKey
      foreingKey: 'category_id',
      as: 'ProductCategory',
    });
Related