I could be extremely wrong from the jump, but I am under the impression that when using GraphQL / Prisma / PostgreSQL as I am, many-to-many relationships can be implicit, however, I am having an extremely hard time doing this and an even harder time finding any helpful documentation online.
I am trying to make a new Model, DailyRoster which is just a date and an array of Driver instances. There needs to be a DailyRoster for every day, thus this makes it a many-to-many relationship, as Drivers can have a DailyRoster for every day, and a DailyRoster will have multiple drivers. This being said, the two models look like this inside of the schema.prisma file.
model Driver {
id String @id @unique @default(uuid())
/ * A whole bunch of irrelevant props * /
ownerId String
owner Owner @relation(fields: [ownerId], references: [id])
managers Manager[]
accidents Accident[]
messages Messages[]
notifiedMessages NotifiedMessages[]
dspId String?
dsp Dsp? @relation(fields: [dspId], references: [id])
weeklyReport WeeklyReport[]
chatrooms Chatroom[]
dailyRosters DailyRoster[]
}
model DailyRoster{
id String @id @unique @default(uuid())
date String @unique
drivers Driver[]
}
There are no errors or red syntax markers / lines on the code here, so I believe this is okay. Then, my mutation function on the backend looks as follows...
const createDailyRoster = async () => {
try{
return await db.dailyRoster.create({
data: {
date: date
}
})
}catch(err){
console.log(err)
}
}
const updateDailyRoster = async (rosterId) => {
return driverIds.forEach( async (driverId) => {
console.log(driverId)
try{
return await db.dailyRoster.update({
where: {
id: rosterId
},
data: {
drivers: {
connect: {
id: driverId
}
}
}
})
}
catch(err){
console.log(err)
}
})
}
Now, whenever I run the mutation and it first creates a DailyRoster with nothing but a date and an ID. Then, using the array of driverIds provided, it runs a forEach loop on the array and is supposed to connect the Drivers to the DailyRoster one by one, but it never gets this far, instead returning the following error on its first driver connection attempt:
An operation failed because it depends on one or more records that were required but not found. Expected 1 records to be connected, found only 0.
Please, someone with a better understanding of GraphQL / Prisma, help me out, am I doing something wrong with the connections or is it a schema related issue?