2015-10-07 30 views
1

我正在使用Sequelize ORM爲我的PostgreSQL關係數據庫。 Sequelize具有nested eager loading的概念,如link中所述,這有助於獲取同一查詢中給定表的關聯。Nested Eager在Sequelize中加載列引用相同的表不工作

DB Schema如下

enter image description here

使用嵌套的立即加載在這個Link提及,但我試圖未能取得任何協會爲我ImageProducts表上ImageProducts

nested include如下:

include:[{ 
      model: models.Tags 
      include: [{ 
       model: models.TagType, 
       }], 
      }]; 

code如下:

var options = { 
     where: { 
      //where condition 
     }, 
     options: [{ 
      model: models.Tags 
      include: [{ 
       model: models.TagType, 
       }], 
      }] 
    } 
    models.ImageProducts.findAll(options).then(function (response) { 
     if (response.length > 0) { 
      //Some Logic 
     } else { 
      // Some other Logic 
     } 
    }).catch(function (err) { 
      // Error Handling 
    }); 

有人可以幫我,爲什麼我不能做nested eager loading目前在sequelize我ImageProducts表,其指的是同一個表,即TagsproductTypepriceRange

回答

0

它應該是:

var options = { 
    where: { 
     //where condition 
    }, 
    include: [{    //<- options is not valid 
     model: models.Tags 
     include: [{ 
      model: models.TagType, 
      }], 
     }] 
} 
+0

你要記得你使用模型的關聯相同的別名添加一個「作爲」屬性。 – Hendrul

+0

嗨Hendrul,我也試過,但仍然無法對給定的模型進行嵌套關聯。 – shubhamagiwal92