1

I have the following Model:

class Profile extends Model {

.......................

  static relationMappings = {
    finishers: {
      relation: Model.ManyToManyRelation,
      modelClass: Move,
      filter: (query) => query.select('move.id', 'move.type'),
      join: {
        from: 'profile.id',
        through: {
          from: 'profile_move.profile_id',
          to: 'profile_move.move_id',
          extra: {
            alternative_name: 'name',
          },
        },
        to: 'move.id',
      },
    },
  };
}

What I'm trying to do is to get only move.id & move.type from the move table and also the extra property name from the profile_move joined table. The problem is that if I use filter or a modifier it returns only move.id & move.type and not the extra property.

Route:

router.get('/', async (req, res, next) => {
  try {
    const data = await Profile.query()
      .orderBy('id')
      .withGraphFetched('finishers');

    res.json(data);
  } catch (error) {
    next(error);
  }
});
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
trunks
  • 147
  • 2
  • 9

1 Answers1

1

The solution is to pass the extra property to filter:

filter: (query) => query.select('move.id', 'move.type', 'profile_move.name'),
trunks
  • 147
  • 2
  • 9
  • Many thanks, unfortunately for me it does not work, I get an unknown column error for "profile_move.name". In the select fields list, the field is replaced with: 'finishers'.'name'as 'finishers:name', which might explain why it says it's unknown. Any idea to make it work? – bfredo123 Aug 10 '23 at 17:21