Deleting User Account and All Related Data
I'm implementing a way for users to delete all their related data using Next.js 14 with App Router. This is my server action to handle data deletion after user confirmation:
This is the frontend function that gets called when confirm is clicked:
8 Replies
What should I do to get rid of this error? The deletion actually works, and it redirects the user to the main page afterward, but having an error like this doesn't seem right.
The problem is that I'm getting an error that says:
Here is part of my prisma schema.
Maybe
onDelete: Cascade
could be a better way to achieve your goal?
https://www.prisma.io/docs/orm/prisma-schema/data-model/relations/referential-actionsReferential actions | Prisma Documentation
Referential actions let you define the update and delete behavior of related models on the database level
Thanks, so basically i sohuld be adding onDelete:cascade to all models that are related to user?
yes
and then delete the user and everything will be deleted aswell
Okey so basically it should be enought that i call
you dont even need the transaction this case
In addition, the error message also says,
Invalid prisma.session.delete()
invocation: An operation failed because it depends on one or more records that were required but not found. Record to delete does not exist.
Notice it says record to delete does not exist. Prisma delete method expects the record to exist and throws an error if it does not.Yeah i think i got The code working. The problem was with The nextauth, it tried to signout The user after The deletion.