简体   繁体   English

Sequelize JS:包含一对多关系

[英]Sequelize JS: Include with one to many relation ship

hi i got a problem i'd like to express below: lets say i have 2 models defined: GameMap and Player, 嗨,我有一个问题要在下面表达:假设我定义了2个模型:GameMap和Player,

GameMap.hasMany(Player, { foreignKey: 'map_id' });

then i would like to perform a query where each Player contains a GameMap object in its query, 那么我想执行一个查询,其中每个玩家的查询中都包含一个GameMap对象,

Player.findAll(){
   include: {
    // how to map Player.map_id onto a GameMap object?
  }
}

i can do it the other way so that GameMap query contains its Players, but id want the opposite of it, couldnt solve this problem using documentation, thx 我可以用另一种方式做到这一点,以便GameMap查询包含其Players,但id想要相反,无法使用文档解决此问题,thx

GameMap.hasMany(Player, { foreignKey: 'map_id'});
Player.belongsTo(GameMap, { foreignKey: 'map_id'});

Both models need to be associated with themselves through the same foreign_key. 两种模型都需要通过相同的foreign_key进行关联。 Now inside Player query I can access its GameMap object. 现在在Player查询中,我可以访问其GameMap对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM