I want to query Exercise model objects and include related objects (relation: athletExerciseLogsExercise). But I need only 1 related object, thats why I add "limit: 1" to inclusion scope:
exerciseController.find({
include: [{
relation: "athletExerciseLogsExercise",
scope: {
where: {
userId: id
},
order: ['date DESC'],
limit: 1
}
}, ]
});
Model
export class Exercise extends Entity {
...
@hasMany(() => AthletExerciseLog, {keyTo: 'exerciseId'})
athletExerciseLogsExercise?: AthletExerciseLog[];
...
}
SQL (from debug)
SELECT
"id",
"userid",
"exerciseid",
"date"
...
FROM
"public"."athletexerciselog"
WHERE
"userid" = '65b9b7110230'
AND "exerciseid" IN (
'd67503d511bb',
'46d156a58aee'
)
ORDER BY "date" DESC
LIMIT 1
Problem: SQL result contains only 1 record for one "exerciseid" = 'd67503d511bb'
Question: how to receive 1 record from "athletexerciselog" for each "exerciseid" ?