我从产品详细信息中获取数据,所以我必须使用不同的表进行多个连接。还有一些依赖的连接,比如product->product\u attribute->attribute\u desc。当我试图用下面的代码获取数据时,它将与product tables列product\u id连接,但我希望它将与attribute\u id连接。
ProductAttribute.hasOne(AttributeDescription, {as: 'AttributeDescription', foreignKey: 'attribute_id',})
AttributeDescription.belongsTo(ProductAttribute, {as: 'ProductAttribute', foreignKey: 'attribute_id'})
Product.findOne({
where: {
status: 1,
product_id: product_id
},
include: [
{
model: ProductDescription,
as: 'ProductDescriptions'
}, {
model: ProductAttribute,
as: 'ProductAttributes',
include: [
{
model: AttributeDescription,
as: 'AttributeDescription'
}
]
}],
})
当前sql示例
SELECT `my_product`.`product_id`, `my_product`.`model`, `my_product`.`sku` FROM `my_product` AS `my_product` LEFT OUTER JOIN `my_product_description` AS `ProductDescriptions` ON `my_product`.`product_id` = `ProductDescriptions`.`product_id` LEFT OUTER JOIN `my_product_attribute` AS `ProductAttributes` ON `my_product`.`product_id` = `ProductAttributes`.`product_id` LEFT OUTER JOIN `my_attribute_description` AS `ProductAttributes->AttributeDescription` ON `ProductAttributes`.`product_id` = `ProductAttributes->AttributeDescription`.`attribute_id` WHERE `my_product`.`status` = 1 AND `my_product`.`product_id` = 999
暂无答案!
目前还没有任何答案,快来回答吧!