Hello! :wave::skin-tone-2: I have some questions a...
# orm-help
s
Hello! 👋🏻 I have some questions about relations...
I have this model:
Copy code
model Clan {
  id          Int    @id @default(autoincrement())
  owner       String @unique
  name        String
  level       Int    @default(1)
  description String @default("[ Описание клана отсутствует! ]")
  picture     String @default("<https://i.imgur.com/OLSFcu5.gif>")
  stars       Int    @default(0)
  voiceTime   Int    @default(0)

  monthActivityId  Int? @unique
  weeklyActivityId Int? @unique

  monthActivity  MonthActivity? @relation(fields: [monthActivityId], references: [id], onDelete: SetNull)
  weeklyActivity WeeklyActivity? @relation(fields: [weeklyActivityId], references: [id], onDelete: SetNull)

  user User[]
}
Activity models looks like:
Copy code
model MonthActivity {
  id        Int @id @default(autoincrement())
  messages  Int @default(0)
  voiceTime Int @default(0)

  isClanActivity Boolean @default(false)

  clan Clan?
  user User?
}
as you can see, i setting null some fields if record about activity was deleted. And now i want to make same for clan. I mean, if i delete clan, i want to delete automatically activity records which binding for that clan by id. How i can implement this by prisma schema, or i should handle it on my own?
j
@Secre if I understand this correctly you'll be wanting to look into referential actions using an onDelete: Cascade option
s
@Jared Fraser Yeah that's it but as you can see i'm already use onDelete thing i can't use it anymore. And right now it works like: if you delete activity, in clan table record with that activity will set null on activityId field. but if i delete clan, activity will be in database anyway. I guess here no way for me to do like two onDelete for models... Idk.
j
you would need to specify the cascade on the activity level. so that when the clan is deleted it will delete the month activity for the clan
Copy code
model MonthActivity {
  ...

  clan Clan? @relation(..., onDelete: Cascade)
}
s
@Jared Fraser yeah 😄 But what if i delete month activity? field with it will remain in clan record ahah 😄
j
you have it to
onDelete: SetNull
, so if you delete the activity the clan record will have a null value for the month activity relation