Skip to content

Commit

Permalink
Enforce unique constraints and cascade deletions for Article.
Browse files Browse the repository at this point in the history
Added unique constraints on "userId, promptedTopic" and "userId, title" in the Article table. Updated foreign key relationships to cascade on delete and update for associated tables. These changes improve data integrity and consistency across related entities.
  • Loading branch information
mikepsinn committed Sep 14, 2024
1 parent 4dde499 commit 73c52ef
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ next-env.d.ts
/test-results/
/dumps/

.trigger
.trigger
/.vscode
17 changes: 17 additions & 0 deletions prisma/migrations/20240914023136_delete_articles/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- DropForeignKey
ALTER TABLE "ArticleGenerationOptions" DROP CONSTRAINT "ArticleGenerationOptions_articleId_fkey";

-- DropForeignKey
ALTER TABLE "ArticleSearchResult" DROP CONSTRAINT "ArticleSearchResult_articleId_fkey";

-- DropForeignKey
ALTER TABLE "ArticleSource" DROP CONSTRAINT "ArticleSource_articleId_fkey";

-- AddForeignKey
ALTER TABLE "ArticleSource" ADD CONSTRAINT "ArticleSource_articleId_fkey" FOREIGN KEY ("articleId") REFERENCES "Article"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ArticleSearchResult" ADD CONSTRAINT "ArticleSearchResult_articleId_fkey" FOREIGN KEY ("articleId") REFERENCES "Article"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "ArticleGenerationOptions" ADD CONSTRAINT "ArticleGenerationOptions_articleId_fkey" FOREIGN KEY ("articleId") REFERENCES "Article"("id") ON DELETE CASCADE ON UPDATE CASCADE;
12 changes: 12 additions & 0 deletions prisma/migrations/20240914023340_unique_articles/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:
- A unique constraint covering the columns `[userId,promptedTopic]` on the table `Article` will be added. If there are existing duplicate values, this will fail.
- A unique constraint covering the columns `[userId,title]` on the table `Article` will be added. If there are existing duplicate values, this will fail.
*/
-- CreateIndex
CREATE UNIQUE INDEX "Article_userId_promptedTopic_key" ON "Article"("userId", "promptedTopic");

-- CreateIndex
CREATE UNIQUE INDEX "Article_userId_title_key" ON "Article"("userId", "title");
9 changes: 6 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,9 @@ model Article {
searchResults ArticleSearchResult[]
generationOptions ArticleGenerationOptions?
comments ArticleComment[]
@@unique([userId, promptedTopic])
@@unique([userId, title])
}

enum ArticleStatus {
Expand Down Expand Up @@ -1486,7 +1489,7 @@ model ArticleSource {
title String
description String?
articleId String
article Article @relation(fields: [articleId], references: [id])
article Article @relation(fields: [articleId], references: [id], onDelete: Cascade)
updatedAt DateTime @updatedAt
createdAt DateTime @default(now())
deletedAt DateTime?
Expand All @@ -1501,7 +1504,7 @@ model ArticleSearchResult {
author String?
text String @db.Text
articleId String
article Article @relation(fields: [articleId], references: [id])
article Article @relation(fields: [articleId], references: [id], onDelete: Cascade)
updatedAt DateTime @updatedAt
createdAt DateTime @default(now())
deletedAt DateTime?
Expand All @@ -1517,7 +1520,7 @@ model ArticleGenerationOptions {
tone String
format String
articleId String @unique
article Article @relation(fields: [articleId], references: [id])
article Article @relation(fields: [articleId], references: [id], onDelete: Cascade)
updatedAt DateTime @updatedAt
createdAt DateTime @default(now())
deletedAt DateTime?
Expand Down

0 comments on commit 73c52ef

Please sign in to comment.