在Sequelize中查询关联表

时间:2016-06-01 14:00:53

标签: javascript sql sequelize.js

我有两个表(usersgames)通过关联表(game_players)连接,创建了多对多关系:

models.Game.belongsToMany(models.User, { through: models.GamePlayer, as: 'players' });
models.User.belongsToMany(models.Game, { through: models.GamePlayer, foreignKey: 'user_id' });

除了外键user_idgame_id之外,game_players还有一些额外的列用于链接特定数据:

sequelize.define('game_player', {
    isReady: {
        defaultValue: false,
        type: Sequelize.BOOLEAN,
        field: 'is_ready'
    },
    isDisabled: {
        defaultValue: false,
        type: Sequelize.BOOLEAN,
        field: 'is_disabled'
    },
    powerPreferences: {
        type: Sequelize.TEXT,
        field: 'power_preferences'
    },
    power: {
        type: Sequelize.STRING(2),
        defaultValue: '?'
    }
}, {
    underscored: true
});

假设我想要获取游戏并热切地加载活跃的玩家。这是我的第一次努力:

db.models.Game.findAll({
    include: [{
        model: db.models.User,
        as: 'players',
        where: { 'game_player.isDisabled': false }
    }]
}).nodeify(cb);

这将生成以下SQL,从而引发错误Column players.game_player.isDisabled does not exist

SELECT "game"."id", 
   "game"."name", 
   "game"."description", 
   "game"."variant", 
   "game"."status", 
   "game"."move_clock"                       AS "moveClock", 
   "game"."retreat_clock"                    AS "retreatClock", 
   "game"."adjust_clock"                     AS "adjustClock", 
   "game"."max_players"                      AS "maxPlayers", 
   "game"."created_at", 
   "game"."updated_at", 
   "game"."gm_id", 
   "game"."current_phase_id", 
   "players"."id"                            AS "players.id", 
   "players"."email"                         AS "players.email", 
   "players"."temp_email"                    AS "players.tempEmail", 
   "players"."password"                      AS "players.password", 
   "players"."password_salt"                 AS "players.passwordSalt", 
   "players"."action_count"                  AS "players.actionCount", 
   "players"."failed_action_count"           AS "players.failedActionCount", 
   "players"."created_at"                    AS "players.created_at", 
   "players"."updated_at"                    AS "players.updated_at", 
   "players.game_player"."is_ready"          AS 
   "players.game_player.isReady", 
   "players.game_player"."is_disabled"       AS 
   "players.game_player.isDisabled", 
   "players.game_player"."power_preferences" AS 
   "players.game_player.powerPreferences", 
   "players.game_player"."power"             AS "players.game_player.power", 
   "players.game_player"."created_at"        AS 
   "players.game_player.created_at", 
   "players.game_player"."updated_at"        AS 
   "players.game_player.updated_at", 
   "players.game_player"."game_id"           AS 
   "players.game_player.game_id", 
   "players.game_player"."user_id"           AS 
   "players.game_player.user_id" 
FROM   "games" AS "game" 
   INNER JOIN ("game_players" AS "players.game_player" 
               INNER JOIN "users" AS "players" 
                       ON "players"."id" = "players.game_player"."user_id") 
           ON "game"."id" = "players.game_player"."game_id" 
              AND "players"."game_player.isdisabled" = false;

显然,Sequelize用不正确的引号包装我的约束别名:'players'。'game_player.isdisabled'应该是'players.game_player'.isdisabled。如何修改上面的Sequelize代码以正确查询此列?

3 个答案:

答案 0 :(得分:1)

我得到了它,但只能通过手动浏览存储库的已关闭的门票并进入#4880

使用不能开箱即用的联接表格列的子句可以包含在$中。老实说,我不明白它的魔力,因为我发誓我没有看到任何文件。修改上面的查询实现了我想要的目标:

db.models.Game.findAll({
    include: [{
        model: db.models.User,
        as: 'players',
        where: { '$players.game_player.is_disabled$': false }
    }]
}).nodeify(cb);

答案 1 :(得分:1)

搜索后,我发现through.where也可以使用:

db.models.Game.findAll({
    include: [{
        model: db.models.User,
        as: 'players',
        through: {  where: { isDisabled: false } }
    }]
})

参考:
Is it possible to filter a query by the attributes in the association table with sequelize?
Eager loading with Many-to-Many relationships

答案 2 :(得分:0)

您的查询应位于加入表中,其中'其中'条件,然后你应该使用' include'包含其他两个模型的子句,如下:

    db.models.GamePlayer.findAll({
        where: {isDisabled: false},
        attributes: [],
        include: [models.User, models.Game]
    }).then(function(result){
        ....
    });