【发布时间】:2021-08-26 00:40:39
【问题描述】:
我有一个产品模型:
module.exports = (sequelize, Sequelize) => {
const Product = sequelize.define("product", {
SKU: {
type: Sequelize.STRING,
},
name: {
type: Sequelize.STRING,
},
description: {
type: Sequelize.TEXT,
},
categoryID: {
type: Sequelize.INTEGER,
}
});
Product.associate = function (models) {
Product.hasMany(models.Stock, { foreignKey: 'productID', as: 'stocks' })
};
return Product;
}
与Stock 模型有hasMany 关系:
module.exports = (sequelize, DataTypes) => {
const Stock = sequelize.define("stock", {
note: DataTypes.STRING,
})
Stock.associate = function (models) {
Stock.belongsTo(models.Product)
};
return Stock;
}
在尝试查询产品时,我收到以下错误:
exports.getAllAvailableProducts = (req, res) => {
const queryParams = req.query;
Product.findAll({
include: ['stocks'],
where: queryParams
}).then((products) => {
res.status(200).send(products);
})
};
(node:58108) UnhandledPromiseRejectionWarning: Error: Association with alias "stocks" does not exist on product
我在这里做错了吗?我也没有看到 FK 出现在我的数据库中
【问题讨论】:
标签: node.js sequelize.js associations