P
Prisma•14h ago
C0casio45

Many to two relation

Hey, I have these model
model a {
id String @id @default(cuid())
b1 b @relation(fields: [b1Id], references: [id]) <- ERR
b2 b @relation(fields: [b2Id], references: [id])

b1Id String
b2Id String
}
model b {
id String @id @default(cuid())
a1 a @relation(fields: [a1Id], references: [id])
a2 a @relation(fields: [a2Id], references: [id])

a1Id String
a2Id String
}
model a {
id String @id @default(cuid())
b1 b @relation(fields: [b1Id], references: [id]) <- ERR
b2 b @relation(fields: [b2Id], references: [id])

b1Id String
b2Id String
}
model b {
id String @id @default(cuid())
a1 a @relation(fields: [a1Id], references: [id])
a2 a @relation(fields: [a2Id], references: [id])

a1Id String
a2Id String
}
the ERR is
Error validating model "a": Ambiguous relation detected. The fields `b1` and `b2` in model `a` both refer to `b`. Please provide different relation names for them by adding `@relation(<name>).
Error validating model "a": Ambiguous relation detected. The fields `b1` and `b2` in model `a` both refer to `b`. Please provide different relation names for them by adding `@relation(<name>).
1 Reply
Prisma AI Help
Prisma AI Help•14h ago
You opted for speed from this bot. Let's conjure up an answer right now! To resolve the ambiguous relation error in your Prisma schema, you need to disambiguate the relations between models a and b by providing unique names for each relation. Here's how you can modify your schema to fix this issue:
model a {
id String @id @default(cuid())
b1 b @relation("aToB1", fields: [b1Id], references: [id])
b2 b @relation("aToB2", fields: [b2Id], references: [id])

b1Id String
b2Id String
}

model b {
id String @id @default(cuid())
a1 a @relation("aToB1", fields: [a1Id], references: [id])
a2 a @relation("aToB2", fields: [a2Id], references: [id])

a1Id String
a2Id String
}
model a {
id String @id @default(cuid())
b1 b @relation("aToB1", fields: [b1Id], references: [id])
b2 b @relation("aToB2", fields: [b2Id], references: [id])

b1Id String
b2Id String
}

model b {
id String @id @default(cuid())
a1 a @relation("aToB1", fields: [a1Id], references: [id])
a2 a @relation("aToB2", fields: [a2Id], references: [id])

a1Id String
a2Id String
}
In this modified schema, we've added unique names to each @relation attribute to disambiguate the relations between models a and b. The relation names "aToB1" and "aToB2" are used to distinguish between the two relations. This approach is based on the concept of disambiguating relations, as described in the Prisma documentation on relations. By adding these unique relation names, you're explicitly telling Prisma how to map the relations between the two models, resolving the ambiguity that was causing the error. Remember that the relation names must be the same on both sides of the relation (in both models) for each specific relation. This solution should resolve the "Ambiguous relation detected" error you were encountering. 🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.

Did you find this page helpful?