Sequlize how to do nested joins

寵の児 提交于 2020-02-06 20:25:47

问题


I have four models. namely

  1. images
  2. restaurant_items
  3. restaurant
  4. item

images.belongsTo (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']
        })

But I want to get the ITEM TABLE AND RESTAURANT TABLE details at the same time

restaurant_items belongs to restaurant

restaurant_items belongs to item

I tried this

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

This didn't worked. Says

Association with alias "res" does not exists

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.

How do I achieve this?


回答1:


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


来源:https://stackoverflow.com/questions/51946774/sequlize-how-to-do-nested-joins

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!