Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create multiple relationships to the same filed in prisma

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.

like image 323
Dulara Malindu Avatar asked Sep 19 '25 01:09

Dulara Malindu


1 Answers

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") 
}
like image 66
Matthias Oertel Avatar answered Sep 23 '25 05:09

Matthias Oertel