Let's say I have these models:
TABLE trainers (
id -- PK
-- many fields here...
);
TABLE players (
id -- PK,
trainer_id -- FK,
-- many fields here...
);
TABLE tournaments (
id -- PK,
player_id -- FK
-- many fields here...
);
TABLE games (
id -- PK,
tournament_id -- PK,
-- many fields here...
);
TABLE goals (
id -- PK,
game_id -- FK
-- many fields here...
);
Many times (using an ORM) I'm having complex queries like:
SELECT
-- many fields here...
"goal"."id",
"goal"."game_id",
-- many fields here...
"game"."tournament_id" AS "game__tournament_id",
-- many fields here...
"game__tournament"."id" AS "game__tournament__id",
"game__tournament"."player_id" AS "game__tournament__player_id",
"game__tournament__player"."trainer_id" AS "game__tournament__player__trainer_id",
-- many fields here...
"game__tournament__player"."id" AS "game__tournament__player__id",
-- many fields here...
FROM
"goals" AS "goal"
LEFT JOIN "games" AS "game" ON ( "game"."id" = "goal"."game_id" )
LEFT JOIN "tournaments" AS "game__tournament" ON ( "game__tournament"."id" = "game"."tournament_id" )
LEFT JOIN "players" AS "game__tournament__player" ON ( "game__tournament__player"."id" = "game__tournament"."player_id" )
WHERE
( "goal"."game_id" IN ( 1, 2, 3 ) )
and all this only because I need the trainer_id
in my service logic that is working with goals
.
What do you suggest?
Should I create a column for trainer_id
in goal
table too? Isn't this repetition bad?
Is there another way to avoid that complex queries?