I have to do the relation between these 2 tables:
model Challenge {
id String @id @default(uuid()) @db.Uuid
user_challenges UserChallenges[]
winners UserChallenges[] @relation("ChallengeWinners")
@@map("challenges")
}
model UserChallenges {
challenge_id String @db.Uuid
user_id String @db.Uuid
challenge Challenge @relation(fields: [challenge_id], references: [id])
winners Challenge? @relation("ChallengeWinners", fields: [challenge_id], references: [id])
@@map("user_challenges")
}
but the @relation in the UserChalenges table return me an error that says:
"Error parsing attribute "@relation": The given constraint name user_challenges_challenge_id_fkey
has to be unique in the following namespace: on model UserChallenges
for primary key, indexes, unique constraints and foreign keys. Please provide a different name using the map
argument.".
It shouldn't work with the alias made? Why is it trying to get the same fkey?
Thanks!
CodePudding user response:
You can't have relations with the same field. In this case, the user_challenges
and winners
may be different users, so you need different fields like this:
model Challenge {
id String @id @default(uuid()) @db.Uuid
user_challenges UserChallenges[]
winners UserChallenges[] @relation("ChallengeWinners")
@@map("challenges")
}
model UserChallenges {
challenge_id String @db.Uuid
challenge_winner_id String @db.Uuid
user_id String @db.Uuid
challenge Challenge @relation(fields: [challenge_id], references: [id])
winners Challenge? @relation("ChallengeWinners", fields: [challenge_winner_id], references: [id])
@@map("user_challenges")
}