2017-04-01 90 views
0

正如標題所述,我試圖弄清楚如何在Sequelize中執行關注/跟隨者關係。我更試圖實現我在Rails中進行的操作。Node Sequelize關注/關係關係

因此,繼承人Ruby代碼

InRelationship Model類文件紅寶石

belongs_to :follower, class_name: "User" 
belongs_to :followed, class_name: "User" 

在用戶模型類文件紅寶石

# Relationships 
has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy 
has_many :following, through: :active_relationships, source: :followed 
has_many :followers, through: :passive_relationships, source: :follower 
has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy 

現在繼承人我翻譯這個代碼到的NodeJS Sequelize 的嘗試User File - NodeJS Sequelize

User.hasMany(models.Relationships, { 
     as: "active_relatinships", 
     foreignKey: "followerId" 
    }) 
User.hasMany(models.Relationships, { 
     as: "passive_relatinships", 
     foreignKey: "followingId" 
    }) 

Rleationship文件的NodeJS Sequelize

Relationships.belongsToMany(models.User, 
     {through: "active_relationships", as: "follower"}) 
Relationships.belongsToMany(models.User, 
     {through: "passive_relationships", as: "followed"}) 

我到底做錯了什麼?這是我試圖包含在我的查詢中時得到的錯誤

未處理的拒絕錯誤:關係(active_relationships)未關聯到用戶!

這裏是我的包含查詢

include: [ 
     {model: models.Areas, as: "current_area" }, 
     {model: models.VisitedAreas}, 
     {model: models.OnTheWays, include: [ 
      {model: models.Areas, as: "Area"}]}, 
     {model: models.Settings}, 
     {model: models.Pictures}, 
     {model: models.Relationships, as: "passive_relationships"}, 
     {model: models.Relationships, as: "active_relationships"}] 

回答

0

我對這個問題的想法!在我的選擇: 你應該使用這個。

User.hasMany(models.Relationships, {foreignKey: "followerId"}) 
    include: [{model: models.Relationships}] 

我寫了一個演示這個問題:

User.hasMany(Money, {foreignKey: "followingId"}); 
    const result = await dbs.User.findAll({include: [{modedbs.Money}]}); 
    console.log(JSON.stringify(result)); 

這是我的結果:enter image description here

+0

這不回答我的問題。我試圖將用戶與名爲關係的模型關聯起來,並且有一個積極的關係,即用戶跟隨和被動關係,這是其他用戶跟隨該用戶 –

+0

@TravisDelly我很抱歉! – MWY

+0

順便說一句,我仍然認爲你不應該使用! – MWY