• <small id='I19Go'></small><noframes id='I19Go'>

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

        • <bdo id='I19Go'></bdo><ul id='I19Go'></ul>
        <legend id='I19Go'><style id='I19Go'><dir id='I19Go'><q id='I19Go'></q></dir></style></legend>

      1. Sequelize如何使用关联表?

        Sequelize how to use association table?(Sequelize如何使用关联表?)
      2. <i id='JsNc1'><tr id='JsNc1'><dt id='JsNc1'><q id='JsNc1'><span id='JsNc1'><b id='JsNc1'><form id='JsNc1'><ins id='JsNc1'></ins><ul id='JsNc1'></ul><sub id='JsNc1'></sub></form><legend id='JsNc1'></legend><bdo id='JsNc1'><pre id='JsNc1'><center id='JsNc1'></center></pre></bdo></b><th id='JsNc1'></th></span></q></dt></tr></i><div id='JsNc1'><tfoot id='JsNc1'></tfoot><dl id='JsNc1'><fieldset id='JsNc1'></fieldset></dl></div>

                <tbody id='JsNc1'></tbody>

              <tfoot id='JsNc1'></tfoot>
            • <small id='JsNc1'></small><noframes id='JsNc1'>

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

                  <legend id='JsNc1'><style id='JsNc1'><dir id='JsNc1'><q id='JsNc1'></q></dir></style></legend>
                  本文介绍了Sequelize如何使用关联表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在使用 Sequelize 时遇到问题,因为我不知道如何解决这个问题.

                  I'm having an issue with Sequelize because I don't know how to approach the problem.

                  我有 3 张桌子:A(游戏)、B(平台)和 AB(游戏平台).A 可以有 1 到多个 B,B 可以有 0 到多个 A.为此,我制作了一个关联表:AB.

                  I have 3 tables : A (game), B(platform) and AB (game_platform). A can have 1 to many B and B can have 0 to many A. To do that I made an association table : AB.

                  在 Sequelize 中,我创建了 A、B、AB 模型,然后我做了:

                  In Sequelize I created the A,B,AB models then I did :

                  db.Game.belongsToMany(db.Platform, {as: 'Game', through: 'GamePlatformsJoin', foreignKey: 'game_platforms_fk_game'});
                  db.Platform.belongsToMany(db.Game, {as: 'Platform', through: 'GamePlatformsJoin', foreignKey: 'game_platforms_fk_platform'});
                  

                  现在是这样的:在我的网站上将创建平台,然后创建游戏.创建游戏时,用户需要定义与之关联的平台.但是我如何告诉 Sequelize 我想在关联表中添加一个新条目?我可以像添加任何其他表格一样添加它,但有没有更简单的方法?

                  Now this is how it will go : On my website platforms will be created and then games. When a game is created the user will need to define the platform(s) associated to it. But how can I tell Sequelize that I want a new entry in my association table ? I can add it as with any other table but is there a simpler way ?

                  推荐答案

                  我的问题的解决方案在 Associating Objects 下的文档中]1(我肯定跳过了).

                  The solution to my problem was in the documentation under Associating Objects]1 (I must have skipped it).

                  这说明如果belingsToMany 配置正确,将动态创建几个方法来管理关联(getX、addX、getXs、addXs...).

                  This explains that if belingsToMany is correctly configured several methods will be dynamically created to mange the association (getX, addX, getXs, addXs,...).

                  我的第二个问题是我在 belongsToMany 中给出的别名,因为我不知道它取了我自己设置的名称并交换了模型的名称.

                  My second issue was the alias I gave in belongsToMany, since I didn't know it took the name of the model I set a name myself and swapped them.

                  现在我删除了别名,它工作正常.

                  Now that I removed the aliases it works fine.

                  db.Game.belongsToMany(db.Platform, {through: db.GamePlatforms, foreignKey: 'game_platforms_fk_game'});
                  db.Platform.belongsToMany(db.Game, {through: db.GamePlatforms, foreignKey: 'game_platforms_fk_platform'});
                  

                  这是我用来测试添加关联"的代码.

                  And here is the code I use to test "add an association".

                  Game.find({where: {game_short: 'SFV'}})
                                .then(function(game) {
                                  Platform.find({where: {platform_short: 'PC'}})
                                    .then(plat => game.addPlatform(plat));
                                })
                                .catch(err => console.log('Error asso game and platform', err));
                  

                  这篇关于Sequelize如何使用关联表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                  Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                  How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                  How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                  How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                  How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)

                  1. <legend id='1u0AX'><style id='1u0AX'><dir id='1u0AX'><q id='1u0AX'></q></dir></style></legend>

                    <small id='1u0AX'></small><noframes id='1u0AX'>

                        • <tfoot id='1u0AX'></tfoot>

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