0

I have some application. There is an Character, which can belong to many group. A group can have many characters and also many ranks. Each Character have specified rank. Ranks differs across the groups.

Character model:

module.exports = (sequelize, DataTypes) => {
  let Character = sequelize.define('Character', {
   /* attributes */
  }, {});
  Character.associate = (models) => {
    Character.hasMany(models.outfit, { foreignKey: 'owner' });
    Character.belongsToMany(models.group, { through: models.groupmember });
  };

  return Character;
};

Group model:

module.exports = (sequelize, DataTypes) => {
  let Group = sequelize.define('Group', {
    /* attributes */
  }, {});
  Group.associate = function (models) {
    Group.belongsToMany(models.character, { through: models.groupmember, as: 'members' });
    Group.hasMany(models.grouprank);
  };
  return Group;
};

GroupMember (junction table) model:

module.exports = (sequelize, DataTypes) => {
  let GroupMember = sequelize.define('GroupMember', {
/* groupId, characterId are generated by sequelize */
    rankId: DataTypes.INTEGER
  }, {});
  GroupMember.associate = function (models) {
    GroupMember.belongsTo(models.grouprank, { foreignKey: 'rankId', targetKey: 'id' });
  };
  return GroupMember;
};

Group rank model: (doesn't matter much in my question)

  module.exports = (sequelize, DataTypes) => {
      let GroupRank = sequelize.define('GroupRank', {
// atributes
      }, {});
      GroupRank.associate = function (models) {
        GroupRank.belongsTo(models.group);
        GroupRank.hasMany(models.groupmember, { foreignKey: 'rankId' });
      };
      return GroupRank;
    };

I have added rankId column to junction table and I have problem to retrieve Character with all its groups included and also the rank which it have. My current code, which returns the Character, the groups to which he belongst to, and also ranks, but the ALL ranks which belongs to the group. And I want just the rank with ID which is specified by rankId.

database.character.findById(characterId, {
    include: [{
      model: database.group,
      through: database.groupmember,
      include: {
        model: database.grouprank,
       /* where: { id: '$GroupMember.rankId$' } */
      }
    }]
  }).then(result => {
  });

Yep. I know it looks weird little, it lacks CamelCase but however my problem is about something else. I tried to query with where attribute, but Sequelize is parsing the string, so I can't put there column name. I hope the question is understable enough. Thank you for your time!

Jakub
  • 50
  • 1
  • 6

1 Answers1

0

I think below code finally worked for me. http://docs.sequelizejs.com/class/lib/sequelize.js~Sequelize.html#static-method-literal

 database.character.findById(characterId, {
    include: [{
      model: database.group,
      through: database.groupmember,
      include: {
        model: database.grouprank,
        where: database.Sequelize.literal('`Groups->GroupRanks`.`id` = `Groups->GroupMember`.`rankId`')
      }
    }]
  })

edit: Finally I decided to rewrite models as stated here -> FindAll with includes involving a complicated many-to-(many-to-many) relationship (sequelizejs)

Jakub
  • 50
  • 1
  • 6