1

Sending categoryID from postman, but unable to save in Product table.

/

/ Products Schema 

module.exports=(sequelize,DataTypes)=>{
    let Products = sequelize.define('Products',{
        productName : DataTypes.STRING,
        unit    : DataTypes.STRING,
        price   : DataTypes.STRING,
    });


    Products.associate = function(models){
        Products.hasMany(models.Categories,{
            foreignKey : 'id',
            as : 'categoryID'
        });
        Products.hasMany(models.Suppliers,{
            foreignKey : 'id',
            as : 'supplierID'
        });
    }
    return Products;
}

Product Controller
 const productCollection = await Product.create({
        productName: req.body.productName,
        unit: req.body.unit,
        price: req.body.price,
        categoryID: req.body.categoryID,
      })
      res.status(200).send(productCollection)



Executing (default): INSERT INTO "Products" ("id","productName","unit","price","createdAt","updatedAt") VALUES (DEFAULT,$1,$2,$3,$4,$5);

Although I have Check in Product Table , categoryID Column exist there. Foreign key is added in Product Table using migration categoryID:{ .... references:{....}}

Yawar ALI
  • 35
  • 6
  • https://github.com/yawar-33/Ecommerce public repo is also updated, is there any one who can help me to solve this issue, as I am beginner on node js – Yawar ALI Jan 18 '21 at 15:02

0 Answers0