0

Basically I have two models:

  1. User
  2. Tournament

they have a Many-to-Many relationship between them through a third model called UserTournament which also have an extra field that track the user points.

and basically for me to grab the data of the users in the tournaments I just write this.

const ladder = await WeeklyLadder.findByPk(req.params.id);
const users = await ladder.getUsers({
  attributes: ['displayName'],
});

but now I'm trying to order it by the user points that are in the 3rd model but I can not seem to do it at all!

I've read in the docs of sequelize that you can add extra fields to the 3rd model and add to them with 'through'

example:

const amidala = User.create({ username: 'p4dm3', points: 1000 });
const queen = Profile.create({ name: 'Queen' });
await amidala.addProfile(queen, { through: { selfGranted: false } });
const result = await User.findOne({
  where: { username: 'p4dm3' },
  include: Profile
});
console.log(result);

but they never explained how to order by this data and its confusing me a lot.

A. Atiyah
  • 515
  • 1
  • 6
  • 16
  • Do you want to order users returned by getUsers()? This answer should point you in the right direction: https://stackoverflow.com/a/36260326/1330105 – macborowy Jun 26 '20 at 20:56
  • Hey @macborowy, yes I do want that but I want to order them by a field in the relationship table called points that isn't really in either model. – A. Atiyah Jun 27 '20 at 02:00

0 Answers0