【问题标题】:Node Sequelize Followering/Following Relationship节点Sequelize追随者/追随者关系
【发布时间】:2017-04-01 02:37:05
【问题描述】:

正如标题所述,我试图弄清楚如何在 Sequelize 中建立追随者/追随者关系。我更想将我在 Rails 中所做的事情实现到 Node 中。

这是 Ruby 代码

InRelationship 模型类文件 Ruby

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

在用户模型类文件Ruby中

# 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 用户文件 - NodeJS Sequelize

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

关系文件 NodeJS 续集

 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"}]

【问题讨论】:

    标签: node.js foreign-keys sequelize.js


    【解决方案1】:

    我对这个问题有个想法!我的选择: 你应该使用它。

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

    这是我的结果:

    【讨论】:

    • 这不能回答我的问题。我正在尝试将用户与名为关系的模型相关联,并且有一个主动关系,即用户关注和被动关系,即其他用户关注该用户
    • @TravisDelly 很抱歉!
    • 顺便说一句,我还是觉得你不应该用 as!
    猜你喜欢
    • 2019-07-03
    • 2017-12-08
    • 2021-10-09
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    相关资源
    最近更新 更多