Node.js Sequelize:为联接查询绑定多个模型并创建自定义列
我想在Sequelize v5中应用一个join和groupBy,它将从五个模型中获取记录,并按以下格式返回记录Node.js Sequelize:为联接查询绑定多个模型并创建自定义列,node.js,sequelize.js,Node.js,Sequelize.js,我想在Sequelize v5中应用一个join和groupBy,它将从五个模型中获取记录,并按以下格式返回记录 { "data": [ { "products": { "id": "01", "name": "lorium", "description": "ipsum", "product_images": [ { "url": "", // From imag
{
"data": [
{
"products": {
"id": "01",
"name": "lorium",
"description": "ipsum",
"product_images": [
{
"url": "", // From images tbl
"image_type": "Front" // From imge_types tbl
},
{
"url": "",
"image_type": "Back"
}
]
},
"vendor": {
"first_name": "john",
"last_name": "doe"
}
}
]
}
我已经分别创建了所有五个模型,并为它们指定了关联
产品型号::
供应商型号::
产品图像模型::
图像模型::
图像类型模型::
下面是我对其执行SQLize操作的存储库文件:Updated::
public async getProductData() {
var prodData = Product.findAll({
include: [
{ model: Vendor, as: 'vendor' },
{ model: ProductImages, as: 'product_img_refs' }
]
});
return prodData;
}
我没有得到绑定所有模型的正确方法,这些模型将返回上述json格式中描述的结果。我发现了问题。您试图将
ProductImages
模型包含到Vendor
中。根据您的关联,ProductImages
与产品
关联,而不是与供应商
关联
所以请试试这个
let prodData = Product.findAll({
include: [
{ model: Vendor, as: 'vendor' },
{ model: ProductImages }
]
});
要获得问题中所示的嵌套输出,需要在以下各项之间创建关联:
和ProductImages
ImagesModel
和ProductImages
imagestypes
findAll
选项中嵌套模型,如下所示:
// Create associations (depends on your data, may be different)
ProductImages.hasOne(ImagesModel);
ProductImages.hasOne(ImageTypes);
// Return product data with nested models
let prodData = Product.findAll({
include: [
{ model: Vendor, as: 'vendor' },
{
model: ProductImages, as: 'product_img_refs',
include: [
{ model: ImagesModel }, // Join ImagesModel onto ProductImages (add aliases as needed)
{ model: ImageTypes } // Join ImageTypes onto ProductImages (add aliases as needed)
]
}
]
});
嗨@darshil,谢谢你点亮了这个东西;我已经在我的问题中更新了此模型关联。。。实际上,我正在寻找创建自定义列的解决方案,我已经用示例json格式显示了这些列。
const ImagesModel = SQLize.define('images', {
id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true, },
img_url: { type: DataTypes.STRING }
});
export { ImagesModel }
const ImageTypes = SQLize.define('image_types', {
id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true, },
image_type: { type: DataTypes.STRING }
});
export { ImageTypes }
public async getProductData() {
var prodData = Product.findAll({
include: [
{ model: Vendor, as: 'vendor' },
{ model: ProductImages, as: 'product_img_refs' }
]
});
return prodData;
}
let prodData = Product.findAll({
include: [
{ model: Vendor, as: 'vendor' },
{ model: ProductImages }
]
});
// Create associations (depends on your data, may be different)
ProductImages.hasOne(ImagesModel);
ProductImages.hasOne(ImageTypes);
// Return product data with nested models
let prodData = Product.findAll({
include: [
{ model: Vendor, as: 'vendor' },
{
model: ProductImages, as: 'product_img_refs',
include: [
{ model: ImagesModel }, // Join ImagesModel onto ProductImages (add aliases as needed)
{ model: ImageTypes } // Join ImageTypes onto ProductImages (add aliases as needed)
]
}
]
});