I have two tables , Parent table is checkout_product and it's child table is product_attributes Now I am creating association between two tables so it's getting error: Here is my code
const Sequelize = require('sequelize');
const { checkout_product_view } = require('../controller/checkoutProductController');
const sequelize = new Sequelize('celeb', 'root', '', {
host: 'localhost',
dialect: 'mysql' ,
});
var Checkout_product = sequelize.define('checkout_products',{
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
name: {
type: Sequelize.STRING
},
price : {
type : Sequelize.STRING
},
image : {
type: Sequelize.STRING
},
quantity : {
type: Sequelize.STRING
},
});
Checkout_product.associate = function(models) {
checkout_products.hasMany(models.Product_attributes, {
foreignKey: 'product_id',
});
};
sequelize.sync()
.catch(error => console.log('This error occured', error));
module.exports = Checkout_product;
And here is my product_attributes model:
const Sequelize = require('sequelize');
const sequelize = new Sequelize('celeb', 'root', '', {
host: 'localhost',
dialect: 'mysql' ,
});
var Product_attribute = sequelize.define('product_attribute',{
id: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
product_id: {
type: Sequelize.INTEGER
},
attribute_name : {
type : Sequelize.STRING
},
attribute : {
type : Sequelize.STRING
},
price : {
type : Sequelize.STRING
}
});
Product_attribute.associate = (models) => {
Product_attribute.belongsTo(models.Checkout_product, {
foreign_key: 'product_id',
sourceKey: 'id'
});
};
sequelize.sync()
.then(() => console.log('Product_attribute'))
.catch(error => console.log('This error occured', error));
module.exports = Product_attribute;
Product has many product_attributes so whenever I called following function getting this error :
module.exports.checkout_product_get = function (req, res) {
Checkout_product.findAll({
include: [ {
model : Product_attributes
}]
})
.then(checkout_product => {
console.log(checkout_product),
res.json(checkout_product)
})
}
Error : UnhandledPromiseRejectionWarning: SequelizeEagerLoadingError: product_attribute is not associated to checkout_products!