Sequelize 如何进行嵌套连接 [英] Sequlize how to do nested joins

查看:53
本文介绍了Sequelize 如何进行嵌套连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个模型.即

  1. 图片
  2. restaurant_items
  3. 餐厅
  4. 项目

images.belongsTo (restaurant_items)

这意味着我可以像这样获取与图像相关的所有restaurant_items表详细信息,

Which means I can get all the restaurant_items table details associated with the image like this,

const all_approved_images = await db.images.findAll({
            where: {
                status: "approved"
            },
            include: ['res_item']
        })

但我想同时获取ITEM TABLE和RESTAURANT TABLE详细信息

restaurant_items 属于餐厅

restaurant_items 属于 item

我试过了

const all_approved_images = await db.images.findAll({
            where: {
                status: "approved"
            },
            include: ['res_item','res','item']
        })

这没有用.说

与别名res"的关联不存在

Association with alias "res" does not exists

因为它不存在于图像但它存在于 restaurant_items

of cause it doesn't exists with the images But it exists with the restaurant_items

应该有一种方法可以将所有这些东西联系起来.

There should be a way to link all this things.

我如何实现这一目标?

推荐答案

开始,从嵌套级别获取数据:

Here you go , getting data from nested levels :

db.images.findAll({
    where: {
        status: "approved"
    },
    include: { 
        association: 'res_item' , // <---- First Level
        include : {
            association: 'res' , // <---- Second Level
            include : {
                association: 'item' , // <---- Third Level
            }
        }
    }
})

这篇关于Sequelize 如何进行嵌套连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆