티스토리 뷰
To set up relations in your Prisma schema, you define connections between models using relation fields and the @relation
attribute. Here’s how you can do it for the main types of relations:
1. One-to-Many Relation
For example, a User
can have many Post
s, but each Post
has only one User
as its author:
model User {
id Int @id @default(autoincrement())
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
author User @relation(fields: [authorId], references: [id])
authorId Int
title String
}
- The
posts
field inUser
and theauthor
field inPost
are relation fields. - The
authorId
field inPost
is a relation scalar field (the foreign key) and is referenced in the@relation
attribute.
Learn more
2. One-to-One Relation
A User
can have one Profile
, and each Profile
belongs to one User
:
model User {
id Int @id @default(autoincrement())
profile Profile?
}
model Profile {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id])
userId Int @unique
}
- The
userId
field inProfile
is marked as@unique
to enforce the one-to-one relationship.
See details
3. Many-to-Many Relation
For example, a Post
can have many Category
entries, and a Category
can have many Post
s:
model Post {
id Int @id @default(autoincrement())
categories Category[]
}
model Category {
id Int @id @default(autoincrement())
posts Post[]
}
- This is an implicit many-to-many relation; Prisma manages the join table for you.
More info
General Notes
- Use the
@relation
attribute to specify which fields are used for the relation and which fields they reference. - Relation fields (like
posts
,author
,profile
) exist only in the Prisma schema and Prisma Client, not in the database. - Relation scalar fields (like
authorId
,userId
) are the actual foreign keys in your database. - For databases that do not support foreign keys (like MongoDB or PlanetScale without FKs), you may need to use the
relationMode = "prisma"
option in your datasource block.
Read about relation modes
If you introspect an existing database, Prisma will attempt to infer relations based on foreign keys and unique constraints it finds in your schema.
See introspection details
For more advanced scenarios, such as disambiguating multiple relations between the same models or using composite keys, refer to the relations documentation.
- Total
- Today
- Yesterday
- System.Diagnostics
- 인텔리제이(intellij)
- In App Purchase
- 스프링 시큐리티(spring security)
- jstl(java standard tag library)-core
- 문자 자르기
- 스프링 프레임워크(spring framewordk)
- 제품 등록
- 특정 문자를 기준으로 자르기
- java.sql
- 스프링 시큐리티(spring security)-http basic 인증
- .submit()
- docker
- 진수 변환
- 스프링 프레임워크(spring framework)
- java web-mvc
- React
- 메이븐(maven)
- jsp 오픈 소스
- 람다식(lambda expression)
- java-개발 환경 설정하기
- jstl(java standard tag library)
- system.io
- await
- error-java
- MainActor
- java 키워드 정리
- 표현 언어(expression language)
- REST API
- nl2br
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |