Skip to content

Commit

Permalink
Add simple schema
Browse files Browse the repository at this point in the history
  • Loading branch information
DemianParkhomenko committed Oct 1, 2023
1 parent 07ec570 commit d3753e6
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 14 deletions.
11 changes: 0 additions & 11 deletions prisma/migrations/20230923140354_init/migration.sql

This file was deleted.

60 changes: 60 additions & 0 deletions prisma/migrations/20231001091908_add_simple_schema/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
28 changes: 25 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit d3753e6

Please sign in to comment.