Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update migration.sql #1144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,52 +1,42 @@
-- CreateTable
-- CreateUserTable
CREATE TABLE "User" (
"id" SERIAL NOT NULL,
"username" TEXT NOT NULL,
"email" TEXT NOT NULL,
"password" TEXT NOT NULL,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")

CONSTRAINT "User_pkey" PRIMARY KEY ("id"),
CONSTRAINT "User_email_key" UNIQUE ("email"), -- Ensure unique email
CONSTRAINT "User_username_key" UNIQUE ("username") -- Ensure unique username
);

-- CreateTable
-- CreatePostsTable
CREATE TABLE "Posts" (
"id" SERIAL NOT NULL,
"title" TEXT NOT NULL,
"body" TEXT NOT NULL,
"userId" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "Posts_pkey" PRIMARY KEY ("id")

CONSTRAINT "Posts_pkey" PRIMARY KEY ("id"),
CONSTRAINT "Posts_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
-- CreateTagsTable
CREATE TABLE "Tags" (
"id" SERIAL NOT NULL,
"tag" TEXT NOT NULL,

CONSTRAINT "Tags_pkey" PRIMARY KEY ("id")

CONSTRAINT "Tags_pkey" PRIMARY KEY ("id"),
CONSTRAINT "Tags_tag_key" UNIQUE ("tag") -- Ensure unique tag name
);

-- CreateTable
-- CreateJoinTableForPostsAndTags
CREATE TABLE "_PostsToTags" (
"A" INTEGER NOT NULL,
"B" INTEGER NOT NULL
"postId" INTEGER NOT NULL,
"tagId" INTEGER NOT NULL,

CONSTRAINT "_PostsToTags_pkey" PRIMARY KEY ("postId", "tagId"),
CONSTRAINT "_PostsToTags_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Posts"("id") ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT "_PostsToTags_tagId_fkey" FOREIGN KEY ("tagId") REFERENCES "Tags"("id") ON DELETE CASCADE ON UPDATE CASCADE
);

-- CreateIndex
CREATE UNIQUE INDEX "Tags_tag_key" ON "Tags"("tag");

-- CreateIndex
CREATE UNIQUE INDEX "_PostsToTags_AB_unique" ON "_PostsToTags"("A", "B");

-- CreateIndex
CREATE INDEX "_PostsToTags_B_index" ON "_PostsToTags"("B");

-- AddForeignKey
ALTER TABLE "Posts" ADD CONSTRAINT "Posts_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_PostsToTags" ADD CONSTRAINT "_PostsToTags_A_fkey" FOREIGN KEY ("A") REFERENCES "Posts"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_PostsToTags" ADD CONSTRAINT "_PostsToTags_B_fkey" FOREIGN KEY ("B") REFERENCES "Tags"("id") ON DELETE CASCADE ON UPDATE CASCADE;