사용자(Users)는 1개의 사용자 정보(UserInfo)를 가지고 있어요.
Users 테이블과 UserInfo 테이블은 1:1 관계를 가지고 있어요!사용자(Users)는 여러개의 게시글(Posts)을 등록할 수 있어요.
Users 테이블과 Posts 테이블을 1:N 관계를 가지고 있어요!사용자(Users)는 여러개의 댓글(Comments)을 작성할 수 있어요.
Users 테이블과 Comments 테이블은 1:N 관계를 가지고 있어요!하나의 게시글(Posts)은 여러개의 댓글(Comments)이 작성될 수 있어요.
Posts 테이블과 Comments 테이블은 1:N 관계를 가지고 있어요!yarn init -y
yarn add express prisma @prisma/client cookie-parser jsonwebtoken
yarn add -D nodemon
npx prisma init
02. [게시판 프로젝트] Prisma 설계하기
[요구사항] 사용자(Users) 테이블
// prisma/schema.prisma
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model Users {
userId Int @id @default(autoincrement()) @map("userId")
email String @unique @map("email")
password String @map("password")
createdAt DateTime @default(now()) @map("createdAt")
updatedAt DateTime @updatedAt @map("updatedAt")
@@map("Users")
}
model Posts {
postId Int @id @default(autoincrement()) @map("postId")
title String @map("title")
content String @map("content") @db.Text
createdAt DateTime @default(now()) @map("createdAt")
updatedAt DateTime @updatedAt @map("updatedAt")
@@map("Posts")
}
model UserInfos {
userInfoId Int @id @default(autoincrement()) @map("userInfoId")
name String @map("name")
age Int? @map("age")
gender String @map("gender")
profileImage String? @map("profileImage")
createdAt DateTime @default(now()) @map("createdAt")
updatedAt DateTime @updatedAt @map("updatedAt")
@@map("UserInfos")
}
model Comments {
commentId Int @id @default(autoincrement()) @map("commentId")
content String @map("content")
createdAt DateTime @default(now()) @map("createdAt")
updatedAt DateTime @updatedAt @map("updatedAt")
@@map("Comments")
}
2) Prisma 1:1 관계
// schema.prisma
model Users {
userId Int @id @default(autoincrement()) @map("userId")
email String @unique @map("email")
password String @map("password")
createdAt DateTime @default(now()) @map("createdAt")
updatedAt DateTime @updatedAt @map("updatedAt")
UserInfos UserInfos? // 사용자(Users) 테이블과 사용자 정보(UserInfos) 테이블이 1:1 관계를 맺습니다.
@@map("Users")
}
model UserInfos {
userInfoId Int @id @default(autoincrement()) @map("userInfoId")
UserId Int @unique @map("UserId") // 사용자(Users) 테이블을 참조하는 외래키
name String @map("name")
age Int? @map("age")
gender String @map("gender")
profileImage String? @map("profileImage")
createdAt DateTime @default(now()) @map("createdAt")
updatedAt DateTime @updatedAt @map("updatedAt")
// Users 테이블과 관계를 설정합니다.
User Users @relation(fields: [UserId], references: [userId], onDelete: Cascade)
@@map("UserInfos")
}
3) Prisma 1:N 연관 관계
// schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model Users {
userId Int @id @default(autoincrement()) @map("userId")
email String @unique @map("email")
password String @map("password")
createdAt DateTime @default(now()) @map("createdAt")
updatedAt DateTime @updatedAt @map("updatedAt")
UserInfos UserInfos? // 사용자(Users) 테이블과 사용자 정보(UserInfos) 테이블이 1:1 관계를 맺습니다.
Posts Posts[] // 사용자(Users) 테이블과 게시글(Posts) 테이블이 1:N 관계를 맺습니다.
Comments Comments[] // 사용자(Users) 테이블과 댓글(Comments) 테이블이 1:N 관계를 맺습니다.
@@map("Users")
}
model Posts {
postId Int @id @default(autoincrement()) @map("postId")
UserId Int @map("UserId") // 사용자(Users) 테이블을 참조하는 외래키
title String @map("title")
content String @map("content") @db.Text
createdAt DateTime @default(now()) @map("createdAt")
updatedAt DateTime @updatedAt @map("updatedAt")
// Users 테이블과 관계를 설정합니다.
User Users @relation(fields: [UserId], references: [userId], onDelete: Cascade)
Comments Comments[] // 게시글(Posts) 테이블과 댓글(Comments) 테이블이 1:N 관계를 맺습니다.
@@map("Posts")
}
model UserInfos {
userInfoId Int @id @default(autoincrement()) @map("userInfoId")
UserId Int @unique @map("UserId") // 사용자(Users) 테이블을 참조하는 외래키
name String @map("name")
age Int? @map("age")
gender String @map("gender")
profileImage String? @map("profileImage")
createdAt DateTime @default(now()) @map("createdAt")
updatedAt DateTime @updatedAt @map("updatedAt")
// Users 테이블과 관계를 설정합니다.
User Users @relation(fields: [UserId], references: [userId], onDelete: Cascade)
@@map("UserInfos")
}
model Comments {
commentId Int @id @default(autoincrement()) @map("commentId")
PostId Int @map("PostId") // 게시글(Posts) 테이블을 참조하는 외래키
UserId Int @map("UserId") // 사용자(Users) 테이블을 참조하는 외래키
content String @map("content")
createdAt DateTime @default(now()) @map("createdAt")
updatedAt DateTime @updatedAt @map("updatedAt")
// Posts 테이블과 관계를 설정합니다.
Post Posts @relation(fields: [PostId], references: [postId], onDelete: Cascade)
// Users 테이블과 관계를 설정합니다.
User Users @relation(fields: [UserId], references: [userId], onDelete: Cascade)
@@map("Comments")
}
4) Prisma DB, Table 생성하기
다시 확인하기!] .env 설정하기!
DATABASE_URL="mysql://root:aaaa4321@express-database.clx5rpjtu59t.ap-northeast-2.rds.amazonaws.com:3306/community_hub"
Prisma DB, Table 생성하기
npx prisma db push