Best setup for cascading?

As part of my app, I have advanced stats for user profiles contained in a seperate table with a foriegn key relation. When deleting a user profile, I need to also delete the row in the advanced stats table (both for practical reasons, and because the process fails if I don't). What's the best way of doing that - I assume it's using onDelete in the schema file?
2 Replies
kapa.ai
kapa.ai2mo ago
Hi @CamBlackwood. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, you are correct. In Prisma, which is used by Wasp, you can use the onDelete: Cascade option in your schema file to automatically delete related records when a parent record is deleted. This is known as a cascading delete. In the context of Wasp, you can see this in action in the Auth and Session entities. In the Auth entity, there is a relation to the User entity with onDelete: Cascade specified. This means that when an Auth entity is deleted, the related User entity will also be deleted. Similarly, in the Session entity, there is a relation to the Auth entity with onDelete: Cascade specified, meaning that when a Session entity is deleted, the related Auth entity will also be deleted. Here is an example from the Auth entity:
entity Auth {=psl
id String @id @default(uuid())
userId Int? @unique
// Wasp injects this relation on the User entity as well
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
identities AuthIdentity[]
sessions Session[]
psl=}
entity Auth {=psl
id String @id @default(uuid())
userId Int? @unique
// Wasp injects this relation on the User entity as well
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
identities AuthIdentity[]
sessions Session[]
psl=}
And here is an example from the Session entity:
entity Session {=psl
id String @id @unique
expiresAt DateTime
userId String
auth Auth @relation(references: [id], fields: [userId], onDelete: Cascade)

@@index([userId])
psl=}
entity Session {=psl
id String @id @unique
expiresAt DateTime
userId String
auth Auth @relation(references: [id], fields: [userId], onDelete: Cascade)

@@index([userId])
psl=}
In your case, you would want to set up a similar relation between your User entity and your AdvancedStats entity, with onDelete: Cascade specified in the relation from User to AdvancedStats. Please note that this will permanently delete the related records. If you want to keep the records for any reason (for example, for historical data or auditing), you might want to consider a different approach, such as setting a deletedAt timestamp and filtering out deleted records in your application logic. Sources: - Auth Entity (https://wasp-lang.dev/docs/auth/entities#auth-entity-) - Session Entity (https://wasp-lang.dev/docs/auth/entities#session-entity-) ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @CamBlackwood, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
martinsos
martinsos2mo ago
Yup this sounds good to me also!
Want results from more Discord servers?
Add your server