Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prisma not deleting because it depends on nonexistent record

I'm using Prisma with an Express backend and React frontend.

Testing my delete request on Postman, I get this error:

"\nInvalid prisma.user.delete() invocation:\n\n\n An operation failed because it depends on one or more records that were required but not found. Record to delete does not exist."

I checked their docs, and I can't find an explanation.

My schema is long, so I'll post the relevant parts:

model User {
  id           String      @id
  email        String      @unique
  firstName    String
  lastName     String
  approved     Boolean     @default(false)
  usersDb      Boolean     @default(false)
  volunteersDb Boolean     @default(false)
  createdAt    DateTime    @default(now())
  updatedAt    DateTime    @updatedAt
  avatarUrl    String?     @default("")
  isActive     Boolean     @default(true)
  lastLoggedIn DateTime    @default(now())
  role         String      @default("viewer")
  volunteer    Volunteer[]
}

model Volunteer {
  id                       String   @id @default(uuid())
  userId                   String
  dbUser                   User     @relation(fields: [userId], references: [id])

My controller

const deleteUser = async (req, res) => {
   const { id } = req.params;
   await prisma.user.delete({
     where: {
       id,
     },
   });

Other than this, the table is behaving as expected and creating the relation to the Volunteer table. I feel like the error has something to do with that because I have another table without a relation, and the delete method works there.

like image 831
techmeowt Avatar asked Nov 28 '25 18:11

techmeowt


2 Answers

You have a one to one relation and you may want to delete the Volunteer when you delete the related User (but that depends on what you want to do). If it is the case, you can add onDelete: Cascade when specifying your relation :

model User {
  id           String      @id
  email        String      @unique
  firstName    String
  lastName     String
  approved     Boolean     @default(false)
  usersDb      Boolean     @default(false)
  volunteersDb Boolean     @default(false)
  createdAt    DateTime    @default(now())
  updatedAt    DateTime    @updatedAt
  avatarUrl    String?     @default("")
  isActive     Boolean     @default(true)
  lastLoggedIn DateTime    @default(now())
  role         String      @default("viewer")
  volunteer    Volunteer[]
}

model Volunteer {
  id                       String   @id @default(uuid())
  userId                   String
  dbUser                   User     @relation(fields: [userId], references: [id], onDelete: Cascade)

Then, when you will delete a user, the corresponding Volunteer will be automatically deleted too.

See documentation for possible actions : https://www.prisma.io/docs/concepts/components/prisma-schema/relations/referential-actions

like image 123
PeO Avatar answered Nov 30 '25 08:11

PeO


I figured this out and wanted to share. I was right, this had everything to do with the relation I'd created between the User and Volunteer table. When I tried to delete a record from the User table, I didn't tell Prisma what to do with the related records, so I got that error. I went back to my Volunteer model and made the relation fields optional, and the delete request worked. Here's the documentation on this, and here's my updated schema:

model User {
  id           String      @id
  email        String      @unique
  firstName    String
  lastName     String
  approved     Boolean     @default(false)
  usersDb      Boolean     @default(false)
  volunteersDb Boolean     @default(false)
  createdAt    DateTime    @default(now())
  updatedAt    DateTime    @updatedAt
  avatarUrl    String?     @default("")
  isActive     Boolean     @default(true)
  lastLoggedIn DateTime    @default(now())
  role         String      @default("viewer")
  volunteer    Volunteer[]
}

model Volunteer {
  id                       String   @id @default(uuid())
  userId                   String?
  dbUser                   User?     @relation(fields: [userId], references: [id])
like image 26
techmeowt Avatar answered Nov 30 '25 07:11

techmeowt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!