jasci
07/11/2021, 10:29 AMonDelete
and onUpdate
behaviour.
We’ve got 3 types of relations: 1-1, 1-n, n-n with 3 possible ways to set up.
Wanted to clarify how to properly add cascading deletes/updates to all of them.
Please correct me if I’m wrong.
1. n-n relations
If I delete Profile, all User records that are related are deleted too.
model User {
id String @id
profiles Profile[] @relation(onDelete: Cascade, onUpdate: Cascade)
}
model Profile {
id String @id
users User[]
}
If I delete User, all Profile records that are related are deleted too.
model User {
id String @id
profiles Profile[]
}
model Profile {
id String @id
users User[] @relation(onDelete: Cascade, onUpdate: Cascade)
}
If I delete User, all Profile records that are related are deleted too. And the other way around.
model User {
id String @id
profiles Profile[] @relation(onDelete: Cascade, onUpdate: Cascade)
}
model Profile {
id String @id
users User[] @relation(onDelete: Cascade, onUpdate: Cascade)
}
2. 1-n relations
If I delete a User, all Profile records that are related are deleted too.
model User {
id String @id
profiles Profile[]
}
model Profile {
id String @id
userId string
user User @relation(fields: [userId], onDelete: Cascade, onUpdate: Cascade)
}
If I delete a Profile, the User record that is related is deleted too.
model User {
id String @id
profiles Profile[] @relation(onDelete: Cascade, onUpdate: Cascade)
}
model Profile {
id String @id
userId string
user User
}
Combination of the previous 2 actions.
model User {
id String @id
profiles Profile[] @relation(onDelete: Cascade, onUpdate: Cascade)
}
model Profile {
id String @id
userId string
user User @relation(fields: [userId], onDelete: Cascade, onUpdate: Cascade)
}
1. 1-1 relations
If I delete a User, the Profile record that is related is deleted too.
model User {
id String @id
profile Profile?
}
model Profile {
id String @id
userId string
user User @relation(fields: [userId], onDelete: Cascade, onUpdate: Cascade)
}
If I delete a Profile, the User record that is related is deleted too.
model User {
id String @id
profile Profile? @relation(onDelete: Cascade, onUpdate: Cascade)
}
model Profile {
id String @id
userId string
user User
}
Combination of the previous 2 actions.
model User {
id String @id
profile Profile? @relation(onDelete: Cascade, onUpdate: Cascade)
}
model Profile {
id String @id
userId string
user User @relation(fields: [userId], onDelete: Cascade, onUpdate: Cascade)
}
Thank you.Ryan
07/12/2021, 6:13 AMposts Post[]
) for e.g. where there’s no reference to a foreign key.Ryan
07/12/2021, 6:13 AMfields
can be defined as that’s the foreign key.jasci
07/12/2021, 9:04 AMonDelete
on the model that we want to delete if some related record is deleted, right?
Like here if User
is deleted all posts of that User are deleted too, right?
model User {
id String @id
posts Post[]
}
model Post {
id String @id
authorId String
author User @relation(fields: [authorId], onDelete: Cascade, onUpdate: Cascade)
}
It’s just with prisma1 afaik onDelete
is put on parent model on the field that we want to delete if that parent model is deleted.
Like that (same behaviour as in prisma2 schema)
type User {
id String @id
posts [Post!] @relation(onDelete: Cascade)
}
type Post {
id String @id
author User @relation(link: TABLE)
}
Sorry for the questions, just want to make sure that I properly refactor the code.Ryan
07/12/2021, 10:24 AMjasci
07/12/2021, 2:25 PM