1

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!

Surbhi Davara
  • 211
  • 1
  • 16
  • I don't see a definition of `checkout_products` in the first module – Anatoly Dec 09 '20 at 13:30
  • In the first file inside `associate` method you have `checkout_products.hasMany` call and you didn't define `checkout_products` in this file above – Anatoly Dec 10 '20 at 06:19
  • I edited product_attributes files to define `checkout_products` !! Please have a look – Surbhi Davara Dec 10 '20 at 06:24
  • In the file where you define `Checkout_product` model you should replace `checkout_products.hasMany` with `Checkout_product.hasMany` because a variable with the model has name `Checkout_product` and not the `checkout_products` – Anatoly Dec 10 '20 at 06:43
  • I updated the model name but still facing the same issue!! `product_attribute is not associated to checkout_products!` – Surbhi Davara Dec 10 '20 at 06:56
  • I see that a line with `checkout_products.hasMany` has not changed to `Checkout_product.hasMany` – Anatoly Dec 10 '20 at 10:16

1 Answers1

0

You didn't call associate methods for all registered models.

I recommend you to define and register all models and associations like I described in this answer

Anatoly
  • 20,799
  • 3
  • 28
  • 42
  • All queries in sequelize working properly so If getting problem like not registered models so that functionality would also not working – Surbhi Davara Dec 09 '20 at 08:32
  • I mean it's better to register all models using functions in model modules altogether and after that call all `associate` functions of registered models. That way you cannot forget to register a new model or its associations – Anatoly Dec 09 '20 at 13:29