diff --git a/prisma/migrations/20221129041646_add_on_delete_to_all_relations/migration.sql b/prisma/migrations/20221129041646_add_on_delete_to_all_relations/migration.sql new file mode 100644 index 0000000..1f7d4cc --- /dev/null +++ b/prisma/migrations/20221129041646_add_on_delete_to_all_relations/migration.sql @@ -0,0 +1,26 @@ +-- DropForeignKey +ALTER TABLE "InvisibleImage" DROP CONSTRAINT "InvisibleImage_imageId_fkey"; + +-- DropForeignKey +ALTER TABLE "InvisibleUrl" DROP CONSTRAINT "InvisibleUrl_urlId_fkey"; + +-- DropForeignKey +ALTER TABLE "Invite" DROP CONSTRAINT "Invite_createdById_fkey"; + +-- DropForeignKey +ALTER TABLE "Url" DROP CONSTRAINT "Url_userId_fkey"; + +-- AlterTable +ALTER TABLE "Url" ALTER COLUMN "userId" DROP NOT NULL; + +-- AddForeignKey +ALTER TABLE "InvisibleImage" ADD CONSTRAINT "InvisibleImage_imageId_fkey" FOREIGN KEY ("imageId") REFERENCES "Image"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Url" ADD CONSTRAINT "Url_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "InvisibleUrl" ADD CONSTRAINT "InvisibleUrl_urlId_fkey" FOREIGN KEY ("urlId") REFERENCES "Url"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Invite" ADD CONSTRAINT "Invite_createdById_fkey" FOREIGN KEY ("createdById") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 8c5bf4e..668af1a 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -56,7 +56,7 @@ model InvisibleImage { id Int @id @default(autoincrement()) invis String @unique imageId Int @unique - image Image @relation(fields: [imageId], references: [id]) + image Image @relation(fields: [imageId], references: [id], onDelete: Cascade) } model Url { @@ -67,15 +67,15 @@ model Url { maxViews Int? views Int @default(0) invisible InvisibleUrl? - user User @relation(fields: [userId], references: [id]) - userId Int + user User? @relation(fields: [userId], references: [id], onDelete: SetNull) + userId Int? } model InvisibleUrl { id Int @id @default(autoincrement()) invis String @unique urlId String @unique - url Url @relation(fields: [urlId], references: [id]) + url Url @relation(fields: [urlId], references: [id], onDelete: Cascade) } model Stats { @@ -90,7 +90,7 @@ model Invite { created_at DateTime @default(now()) expires_at DateTime? used Boolean @default(false) - createdBy User @relation(fields: [createdById], references: [id]) + createdBy User @relation(fields: [createdById], references: [id], onDelete: Cascade) createdById Int }