UserFollows Relation은 Self Relation이고, 나머지는 1:N Relation입니다.
참조: 1:1 Relation
schema.prisma
Nullable은 사용하지 않았습니다. 대신 default 값으로 빈 문자열(@default(value: ""))지정해 주었습니다.
그리고 배열은 아무것도 없는 경우 []를 반환하므로 ?를 사용할 필요도 없고 사용하지도 못합니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model User { | |
id Int @default(autoincrement()) @id | |
userName String @unique | |
avatar String @default(value: "https://3.bp.blogspot.com/-qtEejOg1NHA/Xobmg2y_QeI/AAAAAAAAIVE/UFKPvpeHjKUqCEFOX8lT4MsKz-PwpEGJgCLcBGAsYHQ/s1600/default_user.png") | |
email String @unique | |
firstName String @default(value: "") | |
lastName String @default(value: "") | |
name String @default(value: "") | |
bio String @default(value: "") | |
posts Post[] | |
followers User[] @relation("UserFollows", references: [id]) | |
following User[] @relation("UserFollows", references: [id]) | |
likes Like[] | |
comments Comment[] | |
createdAt DateTime @default(now()) | |
updatedAt DateTime @updatedAt | |
} | |
model Post { | |
id Int @default(autoincrement()) @id | |
user User @relation(fields: [userId], references: [id]) | |
userId Int | |
location String | |
caption String | |
files File[] | |
comments Comment[] | |
likes Like[] | |
createdAt DateTime @default(now()) | |
updatedAt DateTime @updatedAt | |
} | |
model Like { | |
id Int @default(autoincrement()) @id | |
user User @relation(fields: [userId], references: [id]) | |
userId Int | |
post Post @relation(fields: [postId], references: [id]) | |
postId Int | |
createdAt DateTime @default(now()) | |
updatedAt DateTime @updatedAt | |
} | |
model Comment { | |
id Int @default(autoincrement()) @id | |
text String | |
user User @relation(fields: [userId], references: [id]) | |
userId Int | |
post Post @relation(fields: [postId], references: [id]) | |
postId Int | |
createdAt DateTime @default(now()) | |
updatedAt DateTime @updatedAt | |
} | |
model File { | |
id Int @default(autoincrement()) @id | |
url String | |
post Post @relation(fields: [postId], references: [id]) | |
postId Int | |
createdAt DateTime @default(now()) | |
updatedAt DateTime @updatedAt | |
} |
저는 Relation을 통해서 저장소들이 연결되는 통로를 만들었다고 생각합니다.
1:N Relation
- User(1) → Post(N) → User(1)
- User(1) → Like(N) → User(1)
- User(1) → Comment(N) → User(1)
- Post(1) → Like(N) → Post(1)
- Post(1) → Comment(N) → Post(1)
- Post(1) → File(N) → Post(1)
Self Relation
- UserFollows

schema.prisma 파일은 데이터베이스에 테이블을 만들고, 백앤드에서 해당 테이블이 접근하는 Prisma 클라이언트를 만들기 위한 설정 파일일 뿐입니다.
다음 포스트에서 위의 schema.prisma 파일을 사용해서 실제 데이터베이스에 테이블을 생성해 보겠습니다.
'백엔드 > Prisma + GraphQL' 카테고리의 다른 글
Prisma2 클라이언트 생성 (0) | 2020.05.27 |
---|---|
Prisma2 CLI를 사용하여 데이터베이스 테이블 생성 (0) | 2020.05.27 |
Prisma2 데이터 모델링3 - Self Relation (0) | 2020.05.27 |
Prisma2 데이터 모델링2 - 1:N Relation (0) | 2020.05.27 |
Prisma2 데이터 모델링1 - 1:1 Relation (0) | 2020.05.27 |