본문으로 건너뛰기

데이터베이스 스키마 생성하기

이 가이드에서는 프리즈마의 db push 명령을 사용하여 데이터베이스에 테이블을 생성합니다.

다음 프리즈마 데이터 모델을 prisma/schema.prisma의 프리즈마 스키마에 추가합니다.

prisma/schema.prisma
prisma
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String @db.VarChar(255)
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
@@index(authorId)
}
model Profile {
id Int @id @default(autoincrement())
bio String?
user User @relation(fields: [userId], references: [id])
userId Int @unique
@@index(userId)
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
profile Profile?
}
prisma/schema.prisma
prisma
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
title String @db.VarChar(255)
content String?
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId Int
@@index(authorId)
}
model Profile {
id Int @id @default(autoincrement())
bio String?
user User @relation(fields: [userId], references: [id])
userId Int @unique
@@index(userId)
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
profile Profile?
}

이제 새 스키마를 데이터베이스에 푸시할 준비가 되었습니다. 데이터베이스 연결하기의 설명에 따라 main 브랜치와 연결합니다.

이제 db push CLI 명령을 사용하여 main 브랜치로 푸시합니다.

bash
npx prisma db push
bash
npx prisma db push

이제 프리즈마의 db push 명령으로 데이터베이스에 세 개의 테이블을 다음과 같이 만들었습니다.

User

열 이름타입기본 키외래 키필수기본값
idint✔️없음✔️자동 증가
namevarchar(191)없음없음아니-
emailvarchar(191)없음없음✔️-

Post

열 이름타입기본 키외래 키필수기본값
idint✔️없음✔️자동 증가
createdAtdatetime(3)없음없음✔️now()
updatedAtdatetime(3)없음없음✔️
titlevarchar(255)없음없음✔️-
contentvarchar(191)없음없음없음-
publishedtinyint(1)없음없음✔️false
authorIdint없음없음✔️-

Profile

열 이름타입기본 키외래 키필수기본값
idint✔️없음✔️자동 증가
biovarchar(191)없음없음없음-
userIdint없음없음✔️-