diff --git a/prisma/migrations/20230923140354_init/migration.sql b/prisma/migrations/20230923140354_init/migration.sql deleted file mode 100644 index b6a8924..0000000 --- a/prisma/migrations/20230923140354_init/migration.sql +++ /dev/null @@ -1,11 +0,0 @@ --- CreateTable -CREATE TABLE "User" ( - "id" SERIAL NOT NULL, - "email" TEXT NOT NULL, - "name" TEXT, - - CONSTRAINT "User_pkey" PRIMARY KEY ("id") -); - --- CreateIndex -CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); diff --git a/prisma/migrations/20231001091908_add_simple_schema/migration.sql b/prisma/migrations/20231001091908_add_simple_schema/migration.sql new file mode 100644 index 0000000..1827f67 --- /dev/null +++ b/prisma/migrations/20231001091908_add_simple_schema/migration.sql @@ -0,0 +1,60 @@ +-- CreateTable +CREATE TABLE "User" ( + "id" SERIAL NOT NULL, + "username" TEXT NOT NULL, + "email" TEXT NOT NULL, + "password" TEXT NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "User_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Message" ( + "id" SERIAL NOT NULL, + "content" TEXT NOT NULL, + "senderId" INTEGER NOT NULL, + "chatId" INTEGER NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "Message_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "Chat" ( + "id" SERIAL NOT NULL, + "name" TEXT, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "Chat_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "_ChatToUsers" ( + "A" INTEGER NOT NULL, + "B" INTEGER NOT NULL +); + +-- CreateIndex +CREATE UNIQUE INDEX "User_username_key" ON "User"("username"); + +-- CreateIndex +CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); + +-- CreateIndex +CREATE UNIQUE INDEX "_ChatToUsers_AB_unique" ON "_ChatToUsers"("A", "B"); + +-- CreateIndex +CREATE INDEX "_ChatToUsers_B_index" ON "_ChatToUsers"("B"); + +-- AddForeignKey +ALTER TABLE "Message" ADD CONSTRAINT "Message_senderId_fkey" FOREIGN KEY ("senderId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Message" ADD CONSTRAINT "Message_chatId_fkey" FOREIGN KEY ("chatId") REFERENCES "Chat"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "_ChatToUsers" ADD CONSTRAINT "_ChatToUsers_A_fkey" FOREIGN KEY ("A") REFERENCES "Chat"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "_ChatToUsers" ADD CONSTRAINT "_ChatToUsers_B_fkey" FOREIGN KEY ("B") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 7299f5b..3260aeb 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -8,7 +8,29 @@ datasource db { } model User { - id Int @id @default(autoincrement()) - email String @unique - name String? + id Int @id @default(autoincrement()) + username String @unique + email String @unique + password String + messages Message[] @relation("MessageSender") + chats Chat[] @relation("ChatToUsers") + createdAt DateTime @default(now()) +} + +model Message { + id Int @id @default(autoincrement()) + content String + senderId Int + sender User @relation("MessageSender", fields: [senderId], references: [id]) + chatId Int + chat Chat @relation(fields: [chatId], references: [id]) + createdAt DateTime @default(now()) +} + +model Chat { + id Int @id @default(autoincrement()) + name String? + users User[] @relation("ChatToUsers") + messages Message[] + createdAt DateTime @default(now()) }