2016-11-12 65 views
2

我將選擇用戶詳細信息作爲表結構。sequelize js多級表casecade加入

表用戶列{USER_ID,REGION_ID,USER_NAME}

表區域列{REGION_ID,COUNTRY_ID}

表列國家{COUNTRY_ID,COUNTRY_NAME}

我加盟表模型爲

db.user.hasOne(db.region, {foreignKey:"region_id", targetKey:"region_id"}); 
db.region.hasOne(db.country,{ foreignKey :"country_id", targetKey:"country_id" }); 
db.user.hasOne(db.country,{through:db.region,otherKey :"country_id", foreignKey :"user_id" }); 

現在我是goi NG選擇用戶詳細信息包含區域名稱,這裏包含的國家名字是我的控制器

models.user.findAll(
    { 
    include: [ 
     { 
      model: models.region, 
      model: models.country 
     } 
     ] 
}); 

但這個節目的用戶不符合國家相關聯

請救救我

回答

1

應該是多層次的加盟作爲您的模式

models.user.findAll(
    { 
    include: [ 
     { 
      model: models.region, 
      include:[{model:models.country}] 
     } 
     ] 
}); 

前提條件是用戶所屬地區,地區屬於國家 你可以搜索更多關於人際關係的一些例子是

一比一的關係:你作爲一個用戶,可以有一個(hasOne) 檔案。當然,反過來也適用。個人資料(belongsTo)a 用戶。用戶不能有多個配置文件,並且配置文件不能爲 屬於多個用戶。

另外

我們可以定義的使用屬於關聯方法的hasOne關係的反函數。