<i id='XrR43'><tr id='XrR43'><dt id='XrR43'><q id='XrR43'><span id='XrR43'><b id='XrR43'><form id='XrR43'><ins id='XrR43'></ins><ul id='XrR43'></ul><sub id='XrR43'></sub></form><legend id='XrR43'></legend><bdo id='XrR43'><pre id='XrR43'><center id='XrR43'></center></pre></bdo></b><th id='XrR43'></th></span></q></dt></tr></i><div id='XrR43'><tfoot id='XrR43'></tfoot><dl id='XrR43'><fieldset id='XrR43'></fieldset></dl></div>
      <tfoot id='XrR43'></tfoot>

      <small id='XrR43'></small><noframes id='XrR43'>

      <legend id='XrR43'><style id='XrR43'><dir id='XrR43'><q id='XrR43'></q></dir></style></legend>

          <bdo id='XrR43'></bdo><ul id='XrR43'></ul>

        Sequelize - 两个 ID 上的双重连接表

        时间:2023-09-04
          <tbody id='mI4CH'></tbody>
        <tfoot id='mI4CH'></tfoot>

        • <bdo id='mI4CH'></bdo><ul id='mI4CH'></ul>

            <small id='mI4CH'></small><noframes id='mI4CH'>

                <legend id='mI4CH'><style id='mI4CH'><dir id='mI4CH'><q id='mI4CH'></q></dir></style></legend>
                <i id='mI4CH'><tr id='mI4CH'><dt id='mI4CH'><q id='mI4CH'><span id='mI4CH'><b id='mI4CH'><form id='mI4CH'><ins id='mI4CH'></ins><ul id='mI4CH'></ul><sub id='mI4CH'></sub></form><legend id='mI4CH'></legend><bdo id='mI4CH'><pre id='mI4CH'><center id='mI4CH'></center></pre></bdo></b><th id='mI4CH'></th></span></q></dt></tr></i><div id='mI4CH'><tfoot id='mI4CH'></tfoot><dl id='mI4CH'><fieldset id='mI4CH'></fieldset></dl></div>

                1. 本文介绍了Sequelize - 两个 ID 上的双重连接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如果我有一个Users"表和一个Followings"表(Followings"有 UserId 和 FollowedId,都指Users"表中的用户)

                  If I have a "Users" table and a "Followings" table ("Followings" has UserId and FollowedId, both refer to users on the "Users" table)

                  关注"基本上是指一个用户关注"另一个用户

                  "Followings" is basically when one user "follows" another user

                  如何设置关联,以便当我使用 Follows 模型查询和包含 Users 模型时,它将包含 BOTH 列的嵌套信息?

                  How do I set up the associations so that when I use the Followings Model to query and include the Users model, it will include nested info for BOTH columns?

                  例子:

                  Users
                  | id | name | email | etc |
                  
                  Followings
                  | id | UserId | FollowedId |
                  

                  UserId 和 FollowedId 都与Users"表中的用户相关联.

                  UserId and FollowedId are both assciated to users on "Users" table.

                  我试过了:

                  User.hasMany(Following);
                  Following.belongsTo(User, {foreignKey: 'FollowedId'});
                  Following.belongsTo(User, {foreignKey: 'UserId'});
                  

                  但是当我使用时:

                  Followings.find({
                    include: User
                  })
                  

                  它只会在 UserId 上嵌套信息,而不是 FollowedID 用户.

                  It will only nest information on the UserId and not the FollowedID user.

                  如果这令人困惑,请提出问题,谢谢!

                  Please ask questions if this is confusing, thanks!

                  使用 PostgreSQL

                  using PostgreSQL

                  推荐答案

                  我认为如果你给 belongsTo 一个名字,你可以在你的包含中指定关系.所以像:

                  I think if you give a name to the belongsTo you can specify the relationship in your include. So something like:

                  User.hasMany(Following);
                  Following.belongsTo(User, {foreignKey: 'FollowedId', as: 'Followee'});
                  Following.belongsTo(User, {foreignKey: 'UserId', as: 'User'});
                  

                  包含类似:

                  Followings.find({
                    include: [
                      { model: User, as: 'Followee' },
                      { model: User, as: 'User' },
                    ]
                  })
                  

                  这篇关于Sequelize - 两个 ID 上的双重连接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Sequelize:用不同的条件查询同一个连接表 下一篇:如何在 SequelizeJS 中设置创建时通过模型的额外属性?

                  相关文章

                  <small id='tyj0c'></small><noframes id='tyj0c'>

                  <legend id='tyj0c'><style id='tyj0c'><dir id='tyj0c'><q id='tyj0c'></q></dir></style></legend>
                    <i id='tyj0c'><tr id='tyj0c'><dt id='tyj0c'><q id='tyj0c'><span id='tyj0c'><b id='tyj0c'><form id='tyj0c'><ins id='tyj0c'></ins><ul id='tyj0c'></ul><sub id='tyj0c'></sub></form><legend id='tyj0c'></legend><bdo id='tyj0c'><pre id='tyj0c'><center id='tyj0c'></center></pre></bdo></b><th id='tyj0c'></th></span></q></dt></tr></i><div id='tyj0c'><tfoot id='tyj0c'></tfoot><dl id='tyj0c'><fieldset id='tyj0c'></fieldset></dl></div>

                      • <bdo id='tyj0c'></bdo><ul id='tyj0c'></ul>
                    1. <tfoot id='tyj0c'></tfoot>