Reputation: 1637
Here Is the related portion of my datamodel.prisma file.
type Driver {
id: ID! @unique
zones: [Zone!] @relation(name: "DriverZones")
shifts: [Shift!] @relation(name: "DriverShifts")
preferredZone: Zone
preferredShift: Shift
}
type Shift {
id: ID! @unique
drivers: [Driver! ] @relation(name: "DriverShifts")
}
type Zone {
id: ID! @unique
drivers: [Driver! ] @relation(name: "DriverZones")
}
Here I want to create the relationship for preferredZone and preferredShift to be type Zone and Shift according to the datamodel I have created. this is a one way relationship.
The relation field preferredShift
must specify a @relation
directive: @relation(name: "MyRelation")
, The relation field preferredZone
must specify a @relation
directive: @relation(name: "MyRelation")
I'm using PostgreSQL for my prisma database. How to build the relationship between preferredZone to Zone. and preferredShift to Shift.
Upvotes: 4
Views: 9691
Reputation: 167
This section from prisma docs is relevant here:
When you define two relations between the same two models, you need to add the
name
argument in the@relation
attribute to disambiguate them. As an example for why that's needed, consider the following models:// NOTE: This schema is intentionally incorrect. See below for a working solution. model User { id Int @id @default(autoincrement()) name String? writtenPosts Post[] pinnedPost Post? } model Post { id Int @id @default(autoincrement()) title String? author User @relation(fields: [authorId], references: [id]) authorId Int pinnedBy User? @relation(fields: [pinnedById], references: [id]) pinnedById Int? }
In that case, the relations are ambiguous, there are four different ways to interpret them:
User.writtenPosts
↔Post.author
+Post.authorId
User.writtenPosts
↔Post.pinnedBy
+Post.pinnedById
User.pinnedPost
↔Post.author
+Post.authorId
User.pinnedPost
↔Post.pinnedBy
+Post.pinnedById
To disambiguate these relations, you need to annotate the relation fields with the
@relation
attribute and provide thename
argument. You can set anyname
(except for the empty string""
), but it must be the same on both sides of the relation:model User { id Int @id @default(autoincrement()) name String? writtenPosts Post[] @relation("WrittenPosts") pinnedPost Post? @relation("PinnedPost") } model Post { id Int @id @default(autoincrement()) title String? author User @relation("WrittenPosts", fields: [authorId], references: [id]) authorId Int pinnedBy User? @relation("PinnedPost", fields: [pinnedById], references: [id]) pinnedById Int? @unique }
Upvotes: 0
Reputation: 31
I would like you to try this way:
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accountId String @unique
account Account @relation(fields: [accountId], references: [id])
sessions Session[]
TeamHasMembers TeamHasMembers[]
TeamHasLeaders TeamHasLeaders[]
}
model Team {
id String @id @default(cuid())
name String
slug String @unique
TeamHasMembers TeamHasMembers[]
TeamHasLeaders TeamHasLeaders[]
}
model TeamHasMembers {
id String @id @default(cuid())
userId String
teamId String
role String
user User @relation(fields: [userId], references: [id])
team Team @relation(fields: [teamId], references: [id])
}
model TeamHasLeaders {
id String @id @default(cuid())
userId String
teamId String
user User @relation(fields: [userId], references: [id])
team Team @relation(fields: [teamId], references: [id])
}
Upvotes: 0
Reputation: 889
You need to name the relations since you have two relations between same types (Driver <-> Shift and Driver <-> Zone both are connected by two relations each).
In cases like this Prisma asks you to name the relations which is what the error message you posted is about. I think this data model should work:
type Driver {
id: ID! @unique
zones: [Zone!] @relation(name: "DriverZones")
shifts: [Shift!] @relation(name: "DriverShifts")
preferredZone: Zone @relation(name: "PreferredZone")
preferredShift: Shift @relation(name: "PreferredShift")
}
type Shift {
id: ID! @unique
drivers: [Driver! ] @relation(name: "DriverShifts")
}
type Zone {
id: ID! @unique
drivers: [Driver! ] @relation(name: "DriverZones")
}
Upvotes: 8