Cascade delete

This commit is contained in:
Luke Vella 2024-12-17 11:48:57 +00:00
parent 40861f67fc
commit 225d1e6b6a
No known key found for this signature in database
GPG key ID: 469CAD687F0D784C
2 changed files with 50 additions and 11 deletions

View file

@ -0,0 +1,32 @@
-- DropForeignKey
ALTER TABLE "comments" DROP CONSTRAINT "comments_poll_id_fkey";
-- DropForeignKey
ALTER TABLE "options" DROP CONSTRAINT "options_poll_id_fkey";
-- DropForeignKey
ALTER TABLE "participants" DROP CONSTRAINT "participants_poll_id_fkey";
-- DropForeignKey
ALTER TABLE "polls" DROP CONSTRAINT "polls_user_id_fkey";
-- DropForeignKey
ALTER TABLE "watchers" DROP CONSTRAINT "watchers_poll_id_fkey";
-- AddForeignKey
ALTER TABLE "polls" ADD CONSTRAINT "polls_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "watchers" ADD CONSTRAINT "watchers_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "participants" ADD CONSTRAINT "participants_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "options" ADD CONSTRAINT "options_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "votes" ADD CONSTRAINT "votes_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "comments" ADD CONSTRAINT "comments_poll_id_fkey" FOREIGN KEY ("poll_id") REFERENCES "polls"("id") ON DELETE CASCADE ON UPDATE CASCADE;

View file

@ -138,12 +138,13 @@ model Poll {
disableComments Boolean @default(false) @map("disable_comments")
requireParticipantEmail Boolean @default(false) @map("require_participant_email")
user User? @relation(fields: [userId], references: [id])
user User? @relation(fields: [userId], references: [id], onDelete: Cascade)
event Event? @relation(fields: [eventId], references: [id])
options Option[]
participants Participant[]
watchers Watcher[]
comments Comment[]
options Option[] @relation("PollToOptions")
participants Participant[] @relation("PollToParticipants")
watchers Watcher[] @relation("PollToWatchers")
comments Comment[] @relation("PollToComments")
votes Vote[] @relation("PollToVotes")
@@map("polls")
}
@ -169,7 +170,8 @@ model Watcher {
user User @relation(fields: [userId], references: [id])
pollId String @map("poll_id")
createdAt DateTime @default(now()) @map("created_at")
poll Poll @relation(fields: [pollId], references: [id])
poll Poll @relation("PollToWatchers", fields: [pollId], references: [id], onDelete: Cascade)
@@map("watchers")
}
@ -179,15 +181,16 @@ model Participant {
name String
email String?
userId String? @map("user_id")
poll Poll @relation(fields: [pollId], references: [id])
poll Poll @relation("PollToParticipants", fields: [pollId], references: [id], onDelete: Cascade)
pollId String @map("poll_id")
votes Vote[]
locale String?
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime? @updatedAt @map("updated_at")
deleted Boolean @default(false)
deletedAt DateTime? @map("deleted_at")
votes Vote[] @relation("ParticipantToVotes")
@@map("participants")
}
@ -196,7 +199,7 @@ model Option {
startTime DateTime @map("start_time") @db.Timestamp(0)
duration Int @default(0) @map("duration_minutes")
pollId String @map("poll_id")
poll Poll @relation(fields: [pollId], references: [id])
poll Poll @relation("PollToOptions", fields: [pollId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now()) @map("created_at")
@@map("options")
@ -212,7 +215,6 @@ enum VoteType {
model Vote {
id String @id @default(cuid())
participant Participant @relation(fields: [participantId], references: [id], onDelete: Cascade)
participantId String @map("participant_id")
optionId String @map("option_id")
pollId String @map("poll_id")
@ -220,13 +222,15 @@ model Vote {
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime? @updatedAt @map("updated_at")
participant Participant @relation("ParticipantToVotes", fields: [participantId], references: [id], onDelete: Cascade)
poll Poll @relation("PollToVotes", fields: [pollId], references: [id], onDelete: Cascade)
@@map("votes")
}
model Comment {
id String @id @default(cuid())
content String
poll Poll @relation(fields: [pollId], references: [id])
pollId String @map("poll_id")
authorName String @map("author_name")
user User? @relation(fields: [userId], references: [id])
@ -234,6 +238,9 @@ model Comment {
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime? @updatedAt @map("updated_at")
poll Poll @relation("PollToComments", fields: [pollId], references: [id], onDelete: Cascade)
@@map("comments")
}