Relation in prisma is defined as the inter-connection between two models in Prisma schema. For example, if we have a model named User and another named Post, then one user can have many number of posts associated with him/her. It represents 1 to many relation in Prisma. There are three types of relations in Prisma.
Types of relations in Prisma
- One-to-one relations.
- One-to-many relations.
- Many-to-many relations.
- Explicit
- Implicit
One-to-one relations.
To demonstrate one-to-one relations in Prisma, let’s take an example of user and profile model.
We can create these models in Prisma schema as follows:
model User{
id Int @id @default(autoincrement())
email String @unique
profile Profile?
}
model Profile{
name String
gender String
age String
userId Int @unique
user User @relation(fields: [userId], references: [id])
}
In the above code, relation between user and profile is shown. According to this, a user can have zero or one profile(As profile field is option in User model). In Profile model, userId is the foreign key that links that model to User model using the reference of Id.
One-to-many relations.
For one-to-many relations, let’s take an example of User and Post model. To relate these models, it can be defined is prisma schema as:
model User{
id Int @id @default(autoincrement())
email String @unique
posts Post[]
}
model Post{
title String
userId Int @unique
user User @relation(fields: [userId], references: [id])
}
In the given example, a User can have many number of Posts. In this case, userId in Post model acts as the foreign key to relate Post to User model with the reference of “id”. Similarly, in User model, you have to define posts as an array of Post i.e. posts Post[].
Many-to-many relations.
To represent many-to-many relations, lets take an example of Post and Tag model. As the name suggests, many posts can be associated with many tags. Now, it can be done in two different ways i.e Implicitly and explicitly.
Explicit many-to-many relations:
In explicit m-m relations, we have to create a intermediary table explicitly that helps to relate the two models(Post and tag-in our case). It can be done as follow:
model Post{
id Int @id @default(autoincrement())
title String
tags PostTag[]
}
model Tag{
id Int @id @default(autoincrement())
post PostTag[]
}
model PostTag{
postId Int
post Post @relation(fields:[postId], references:[id])
tagId Int
tag Tag @relation(fields:[tagId], references:[id])
@@id([postId,tagId])
}
Here, the model PostTag acts as the middleman/intermediary model that connects Post and Tag model using postId and tagId as defined in PostTag model.
Implicit many-to-many relations.
In case of implicit many-to-many relations, we don’t have to create any intermediary table that connects two model to each other. But, in this case, Prisma creates the intermediary table and names it on its own under the hood for us. We have to define the fields in both models as lists. It can be practised as follow:
model Post{
id Int @id @default(autoincrement())
title String
tags Tag[]
}
model Tag{
id Int @id @default(autoincrement())
posts Post[]
}
So, this is all you need to know about relations in prisma. To know more about relations in prisma and how you can query them, you can visit their official docs by clicking HERE.