Prisma schema
The Prisma schema file is the main configuration file for your Prisma setup. It is typically called schema.prisma and consists of the following parts:
- Data sources: Specify the details of the data sources Prisma should connect to (e.g. a PostgreSQL database)
- Generators: Specifies what clients should be generated based on the data model (e.g. Prisma Client)
- Data model definition: Specifies your application models (the shape of the data per data source) and their relations
Whenever a prisma command is invoked, the CLI typically reads some information from the schema file, e.g.:
- prisma generate: Reads all above mentioned information from the Prisma schema to generate the correct data source client code (e.g. Prisma Client).
- prisma migrate dev: Reads the data sources and data model definition to create a new migration.
Data Model
- Database의 테이블에 매핑되는 개념.
- Field (DB의 column과 동일), Filed Type (DB의 data_type과 동일), 제약조건 (unique, realation(1:n, n:m 관계)) 으로 이루어져 있음.
Defining an ID field
model User {
id Int @id @default(autoincrement())
}
- In relational databases, the ID can be a single field or based on multiple fields. If a model does not have an @id or an @@id, you must define a mandatory @unique field or @@unique block instead.
- autoincrement()는 해당 필드를 자동으로 증가하는 값으로 설정합니다.
- cuid()는 cuid 사양에 따라 고유한 식별자를 생성합니다.
- uuid()는 uuid 사양에 따라 고유한 식별자를 생성합니다.
- now()는 생성된 시간의 시간을 기록합니다.
Defining a default value
model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
published Boolean @default(false)
}
Default values can be:
- Static values that correspond to the field type, such as 5 (Int), Hello (String), or false (Boolean)
- Lists of static values, such as [5, 6, 8] (Int[]) or ["Hello", "Goodbye"] (String[]).
- Functions, such as now() or uuid()
Relation fields
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 // relation scalar field (used in the `@relation` attribute above)
}
- A relation is a connection between two models in the Prisma schema. For example, there is a one-to-many relation between User and Post because one user can have many blog posts.
- The following Prisma schema defines a one-to-many relation between the User and Post models.
- At a Prisma level, the User / Post relation is made up of:
- Two relation fields: author and posts. Relation fields define connections between models at the Prisma level and do not exist in the database. These fields are used to generate Prisma Client.
- The scalar authorId field, which is referenced by the @relation attribute. This field does exist in the database - it is the foreign key that connects Post and User.
Optional and mandatory fields
model Comment {
id Int @id @default(autoincrement())
title String
content String?
}
- When not annotating a field with the ? type modifier, the field will be required on every record of the model.
참고
https://velog.io/@ltnscp9028/Prisma-Model-넌-누구니
https://www.prisma.io/docs/concepts/components/prisma-schema
https://www.prisma.io/docs/concepts/components/prisma-schema/data-model
https://www.prisma.io/docs/concepts/components/prisma-schema/relations
회고
prisma 공식 문서 너무너무 친절해서 약간 감동받음 ㅋㅋ 오늘 느낀 prisma 꿀팁 -> prisma schema 수정할 때는 `npx prisma format`을 생활화하자. 사전에 에러 잡아주고 말 그대로 매우 유용함! 오늘도 예약 기능 구현 마치고 생산적인 하루를 보냈다. 🥰