Search code examples
postgresqlgraphqlmikro-orm

mikro orm forget the relationship between two entities when i restart the server


i have two entities 'Game' and 'GameMode' a game can have multiple game modes and a game mode belongs to a game

the issue is whenever i create a game and a game mode for it everything works fine until i restart the server then the relationship between the two disappears and i only get the game without it gameModes

Creating a game

    @Mutation(() => GeneralResponse)
     async createGame(@Ctx() { em }: MyContext,
     @Arg("name") name: string,
     ): Promise<GeneralResponse> {
        const game = em.create(Game, {
              active: true,
              category: Category.SPORTS,
              name: name,
        } as Game);
     await em.persistAndFlush(game);
     return { success: true };
    }

Creating a gameMode for it

    @Mutation(() => GeneralResponse)
      @UseMiddleware(Authentication)
      @UseMiddleware(Admin)
      async createGameMode(@Ctx() { em }: MyContext,
        @Arg("gameId") gameId: number,
        @Arg("name") name: string
      ): Promise<GeneralResponse> {
        const gameMode = em.create(GameMode, {
          Game: em.getReference(Game,gameId),
          name: name
        } as any);
        await em.persistAndFlush(gameMode);
        return { success: true };
      }

Game Entity

GameMode Entity

This is what happens when i restart the server

GraphQL response after i restart the server

i have tried to write raw sql for it it did not work same issue


Solution

  • I managed to fix this issue by adding the "populate"

    @Query(() => [Game], { nullable: true })
      async games(@Ctx() { em }: MyContext): Promise<any> {
        const games = await em.find(Game, {}, {
          populate: ['gameModes'],
        });
        return games;
      }