gpt4 book ai didi

javascript - 在 Sequelize 中查询关联表

转载 作者:行者123 更新时间:2023-11-30 00:08:17 24 4
gpt4 key购买 nike

我有两个表(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 代码以正确查询此列?

最佳答案

我明白了,但只能通过手动浏览存储库的已关闭票证并找到 #4880 .

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

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

关于javascript - 在 Sequelize 中查询关联表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37570926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com