[Prisma] Prisma 관계

김민재·2023년 11월 29일

Prisma

목록 보기
4/7

1:1 관계 UserInfos UserInfos? // 1:1 관계

@id: 해당 필드가 기본 키 (Primary Key) 역할을 한다는 것을 나타냅니다.

model Users {
  userId  Int  @id @default(autoincrement()) @map("userId")
  email String @map("email")
  password String @map("password")
  createdAt DateTime @default(now()) @map("createdAt")
  updatedAt DateTime @updatedAt @map("updatedAt")

  UserInfos UserInfos? // 1:1 관계 

  @@map("Users")
}

model UserInfos {
  userInfoId   Int     @id @default(autoincrement()) @map("userInfoId")
  UserId Int @unique @map("UserId")
  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")

  User Users @relation(fields: [UserId], references: [userId], onDelete: Cascade)

  @@map("UserInfos")
}
  • 참조 하고 있는 테이블에서 외래키를 설정하기 위해서 fields: [''] 하면 된다.

1:n 관계 Posts Posts[] // 1:n 관계

profile
개발 경험치 쌓는 곳

0개의 댓글