@ridhwaans 👋
delete will always throw an exception so it's best to handle it via try/catch to keep it to a single query.
Support `deleteIfExists()` and nested `deleteIfExists`
new guy with wierd error when deleting a record trough prisma studio
node.js - Prisma not deleting because it depends on nonexistent record - Stack Overflow
Error on delete with zero records
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
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])
I’m writing v2 of an app I maintain and am considering moving from Sequelize to Primsa.
From my initial investigations I really like Primsa’s typescript support and the fact it returns plain JavaScript objects rather than complex model classes. However I’ve also encountered a few weirdnesses and quirks that have set my spidey-sense tingling and given me pause about adopting it.
In particular, the fact it generates custom client code to your node_modules seems very unusual (at least I’ve never seen this before) and ripe for causing unexpected, hard to debug problems - e.g. when caching dependencies on a build server.
It also seems strangely hard to have a createdBy column on database tables without an incredible amount of boilerplate setting up inverse relationships on your User model for every single other model in your schema.
And (I think a common complaint) not being able to split up your schema file is quite annoying.
I’ve only been toying with it for a couple of days and I’ve already run into these three wrinkles, which suggests to me that if I were to carry on, I might run into more. Is that the case? Are there other rough edges I should expect to encounter? It seems to make a lot of helpful things much easier, which is great. What does it make harder?
I’m also interested in hearing about any unexpected problems you ran into using Prisma in production - things it would be hard for me to figure out/predict from first principles and are only really learned through painful experience.
Asking here rather than on a Prisma sub in the hope of getting a wide range of responses. But I may ask there as well. Thanks!