Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

sequelize.js - Sequlize how to do nested joins

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...