작성
·
16
0
💡 ”로펀의 인프런 상담소” 슬랙 채널 가입하기 💡
평일중에는 퇴근 이후(저녁 7시)에 답변을 받아보실 수 있고, 주말중에는 상시 답변드리고 있습니다.
안녕하세요 . 강의 수강중 질문이 생겨서 여기에 이렇게 남깁니다.
Courses 테이블에서 Lectures 테이블과 일대다 관계를 이렇게 해주셨는데요.
model Course {
id String @id @default(uuid())
slug String @unique
title String
shortDescription String? @map("short_description")
description String? @map("description")
thumbnailUrl String? @map("thumbnail_url")
price Int @default(0)
discountPrice Int? @map("discount_price")
level String @default("beginner")
instructorId String @map("instructor_id")
isPublished Boolean @default(false) @map("is_published")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
sections Section[]
lectures Lecture[]
categories CourseCategory[]
enrollments CourseEnrollment[]
reviews CourseReview[]
questions CourseQuestion[]
instructor User @relation(fields: [instructorId], references: [id])
@@map("courses")
}
model Section {
id String @id @default(uuid())
title String
description String?
order Int
courseId String @map("course_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
lectures Lecture[]
course Course @relation(fields: [courseId], references: [id], onDelete: Cascade)
@@map("sections")
}
model Lecture {
id String @id @default(uuid())
title String
description String?
order Int
duration Int?
isPreview Boolean @default(false) @map("is_preview")
sectionId String @map("section_id")
courseId String @map("course_id")
videoStorageInfo Json? @map("video_storage_info")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
section Section @relation(fields: [sectionId], references: [id], onDelete: Cascade)
course Course @relation(fields: [courseId], references: [id], onDelete: Cascade)
activities LectureActivity[]
@@map("lectures")
}
궁금한점은 이미 저희가 나중에 DTO 에서 코스 테이블 레코드 를 가져오면 Sections 테이블과 이미 일대다 관계가 되어있기떄문에 Sections 테이블에서 레코드를 가져오면서 Sections 테이블이 이미 Lectures 와 일대다 관계가 되어있기 떄문에 코스 테이블에서 레코드 가져오면 섹션 테이블 안에 이미 렉쳐 레코드도 같이 따라오는데 코스 테이블에서 따리 중독 관계를 해두신 이유가 궁금합니다.
시간 내주셔서 감사합니다
답변 1
0
안녕하세요, 인프런 AI 인턴입니다. Gyubum Kim님.
Prisma 스키마에서 특정 테이블 간의 관계 설정은 데이터베이스에서 데이터를 효율적으로 조회하고 관리하기 위해 매우 중요합니다. Course 테이블에서 Lecture 테이블과의 관계를 따로 설정한 이유는 특정 상황에서 Course 객체로부터 직접적으로 Lecture 인스턴스들을 가져오고자 할 때 유용하기 때문입니다. 이렇게 두 테이블의 관계를 명시적으로 설정해 두면, 데이터베이스 쿼리를 작성할 때 보다 직접적이고 편리하게 데이터를 관리할 수 있습니다.
예를 들어, 특정 강의(Course)에 속한 모든 강의 자료(Lecture)를 조회해야 할 경우 Sections를 통해 접근하는 것보다 직접적으로 Course -> Lectures로 접근하는 것이 더 논리적인 상황일 수 있습니다. 이러한 구성을 통해, 동일한 데이터에 대해 다양한 관점으로 접근이 가능해지며, 이는 복잡한 데이터 구조를 처리할 때 매우 도움이 됩니다.
질문이 해결되기를 바라며, 추가적인 문의 사항이 있으시면 언제든지 슬랙 채널을 통해 질문 주시면 감사하겠습니다!
즐거운 학습 되세요!