【问题标题】:SequelizeEagerLoadingError : associated not working in sequelizeSequelizeEagerLoadingError : 关联在 sequelize 中不起作用
【发布时间】:2020-12-10 14:23:30
【问题描述】:

我有两个表,父表是 checkout_product,它的子表是 product_attributes 现在我正在创建两个表之间的关联,因此出现错误: 这是我的代码

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;

这是我的 product_attributes 模型:

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_attributes 所以每当我调用以下函数时都会收到此错误:

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

错误:UnhandledPromiseRejectionWarning: SequelizeEagerLoadingError: product_attribute is not associated to checkout_products!

【问题讨论】:

  • 我在第一个模块中没有看到checkout_products 的定义
  • associate 方法内的第一个文件中,您有checkout_products.hasMany 调用,但您没有在上面的此文件中定义checkout_products
  • 我编辑了product_attributes文件来定义checkout_products!!请看一下
  • 在您定义Checkout_product 模型的文件中,您应该将checkout_products.hasMany 替换为Checkout_product.hasMany,因为模型变量的名称为Checkout_product 而不是checkout_products
  • 我更新了型号名称,但仍然面临同样的问题!! product_attribute is not associated to checkout_products!

标签: mysql node.js sequelize.js


【解决方案1】:

您没有为所有注册模型调用associate 方法。

我建议您像我在 answer 中描述的那样定义和注册所有模型和关联

【讨论】:

  • sequelize 中的所有查询都可以正常工作,因此如果遇到未注册模型等问题,那么功能也将无法正常工作
  • 我的意思是最好使用模型模块中的函数注册所有模型,然后调用注册模型的所有associate函数。这样您就不会忘记注册新模型或其关联
猜你喜欢
  • 2022-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-22
  • 2018-12-18
  • 2018-10-23
  • 1970-01-01
相关资源
最近更新 更多