#Set Null when relation is deleted

1 messages · Page 1 of 1 (latest)

crimson fable
#

I want to set the pdfId and the imageId of the release note to null when a file is getting deleted. I get the following issue

Error validating: When any of the records in model File is updated or deleted, the referential actions on the relations cascade to model ReleaseNote through multiple paths. Please break one of these paths by setting the onUpdate and onDelete to NoAction

Any ideas on that?

  id              Int    @id(map: "PK_FILE_ID") @default(autoincrement())
  ......
  imageOfReleaseNotes ReleaseNote[] @relation("imageOfReleaseNotes")
  pdfOfReleaseNotes   ReleaseNote[] @relation("pdfOfReleaseNotes")

  @@map("file")
  @@schema("dbo")
}

model ReleaseNote {
  id       Int    @id(map: "PK_RELEASE_NOTE_ID") @default(autoincrement())
  ....
  releaseNoteToUsers ReleaseNoteToUser[]

  pdf   File? @relation("pdfOfReleaseNotes", fields: [pdfId], references: [id], onDelete: SetNull, onUpdate: NoAction, map: "FK_RELEASE_NOTE_PDF_FILE")
  pdfId Int?

  image   File? @relation("imageOfReleaseNotes", fields: [imageId], references: [id], onDelete: SetNull, onUpdate: NoAction, map: "FK_RELEASE_NOTE_IMAGE_FILE")
  imageId Int?

  @@map("release_note")
  @@schema("dbo")
}
bronze mangoBOT
#

You've gone full bot mode! Let me fetch that AI answer for you ASAP.

bronze mangoBOT
# bronze mango You've gone full bot mode! Let me fetch that AI answer for you ASAP.

You’re hitting Prisma’s protection against multiple cascade paths: there are two relations from File to ReleaseNote (pdfOfReleaseNotes and imageOfReleaseNotes), and both default to cascading on update/delete unless you explicitly break one path with NoAction. [Special rules]

To keep your desired SetNull behavior for one relation, you must set onDelete/onUpdate to NoAction on the other relation so there is only one cascade path.

For example, if you only need SetNull for pdf:

model ReleaseNote {
  id       Int    @id(map: "PK_RELEASE_NOTE_ID") @default(autoincrement())
  // ...

  pdf   File? @relation(
    "pdfOfReleaseNotes",
    fields: [pdfId],
    references: [id],
    onDelete: SetNull,
    onUpdate: NoAction,
    map: "FK_RELEASE_NOTE_PDF_FILE"
  )
  pdfId Int?

  image   File? @relation(
    "imageOfReleaseNotes",
    fields: [imageId],
    references: [id],
    onDelete: NoAction,
    onUpdate: NoAction,
    map: "FK_RELEASE_NOTE_IMAGE_FILE"
  )
  imageId Int?
}

If you really need both to SetNull, Prisma’s validation rules don’t document a way to allow two such paths at once; you’d have to pick one to be handled by the database (SetNull) and handle the other in application code (manually nulling imageId or pdfId before deleting the File). [Special rules; SetNull]

bronze mangoBOT