Hello everybody. Wanted to clear some things up re...
# orm-help
j
Hello everybody. Wanted to clear some things up regarding new 
onDelete
 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.
Copy code
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.
Copy code
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.
Copy code
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.
Copy code
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.
Copy code
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.
Copy code
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.
Copy code
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.
Copy code
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.
Copy code
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.
r
@jasci 👋 Cascades will not work on the many side of N-many relations (
posts Post[]
) for e.g. where there’s no reference to a foreign key.
It will only work on the model where the
fields
can be defined as that’s the foreign key.
j
Got it, also now we put
onDelete
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?
Copy code
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)
Copy code
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.
r
Yup that’s correct 💯
j
Thank you
👍 1