Friday, 21 July 2023

drizzle-orm with multiple many-to-one relations - Please specify relation name at normalizeRelation

I have two tables and two relations that I have defined as follows:

export const games = pgTable('games', {
    id: serial('id').primaryKey(),
    player1Id: integer('player1_id'),
    player1Scores: json('player1_scores'),

    player2Id: integer('player2_id'),
    player2Scores: json('player2_scores'),

    winnerId: integer('winner_id'),
});

export const players = pgTable('players', {
    id: serial('id').primaryKey(),
    email: varchar('email'),
    name: varchar('name', { length: 256 }),
});

export const playersRelations = relations(players, ({ many }) => ({
    games: many(games),
}));

export const gamesRelations = relations(games, ({ one }) => ({
    player1: one(players, { fields: [games.player1Id], references: [players.id] }),
    player2: one(players, { fields: [games.player2Id], references: [players.id] }),
    winner: one(players, { fields: [games.winnerId], references: [players.id] }),
}));

However, I get the error message with the gamesRelations that there are "multiple relations between 'games' and 'players'). If I remove the entry with 'player2' and 'winner' from the gamesRelation, it works. But then the information is missing.

Probably the error is relatively simple, but I have no more ideas and am grateful for any help.

Error message:

There are multiple relations between "games" and "players". Please specify relation name at normalizeRelation



from drizzle-orm with multiple many-to-one relations - Please specify relation name at normalizeRelation

No comments:

Post a Comment