apps/docs/content/docs.v6/orm/prisma-schema/data-model/relations/index.mdx
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. The fields involved in defining the relation are highlighted:
model User {
id Int @id @default(autoincrement())
posts Post[] // [!code highlight]
}
model Post {
id Int @id @default(autoincrement())
author User @relation(fields: [authorId], references: [id]) // [!code highlight]
authorId Int // relation scalar field (used in the `@relation` attribute above) // [!code highlight]
title String
}
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
posts Post[] // [!code highlight]
}
model Post {
id String @id @default(auto()) @map("_id") @db.ObjectId
author User @relation(fields: [authorId], references: [id]) // [!code highlight]
authorId String @db.ObjectId // relation scalar field (used in the `@relation` attribute above) // [!code highlight]
title String
}
At a Prisma ORM level, the User / Post relation is made up of:
author and posts. Relation fields define connections between models at the Prisma ORM level and do not exist in the database. These fields are used to generate Prisma Client.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.At a Prisma ORM level, a connection between two models is always represented by a relation field on each side of the relation.
<div class="videoWrapper"> <iframe width="560" height="315" src="https://www.youtube.com/embed/fpBYj55-zd8" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen ></iframe> </div>The following entity relationship diagram defines the same one-to-many relation between the User and Post tables in a relational database:
In SQL, you use a foreign key to create a relation between two tables. Foreign keys are stored on one side of the relation. Our example is made up of:
Post table named authorId.User table named id. The authorId column in the Post table references the id column in the User table.In the Prisma schema, the foreign key / primary key relationship is represented by the @relation attribute on the author field:
author User @relation(fields: [authorId], references: [id])
Note: Relations in the Prisma schema represent relationships that exist between tables in the database. If the relationship does not exist in the database, it does not exist in the Prisma schema.
For MongoDB, Prisma ORM currently uses a normalized data model design, which means that documents reference each other by ID in a similar way to relational databases.
The following document represents a User (in the User collection):
{ "_id": { "$oid": "60d5922d00581b8f0062e3a8" }, "name": "Ella" }
The following list of Post documents (in the Post collection) each have a authorId field which reference the same user:
[
{
"_id": { "$oid": "60d5922e00581b8f0062e3a9" },
"title": "How to make sushi",
"authorId": { "$oid": "60d5922d00581b8f0062e3a8" }
},
{
"_id": { "$oid": "60d5922e00581b8f0062e3aa" },
"title": "How to re-install Windows",
"authorId": { "$oid": "60d5922d00581b8f0062e3a8" }
}
]
This data structure represents a one-to-many relation because multiple Post documents refer to the same User document.
@db.ObjectId on IDs and relation scalar fieldsIf your model's ID is an ObjectId (represented by a String field), you must add @db.ObjectId to the model's ID and the relation scalar field on the other side of the relation:
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
posts Post[] // [!code highlight]
}
model Post {
id String @id @default(auto()) @map("_id") @db.ObjectId
author User @relation(fields: [authorId], references: [id])
authorId String @db.ObjectId // relation scalar field (used in the `@relation` attribute above) // [!code highlight]
title String
}
Prisma Client is generated from the Prisma schema. The following examples demonstrate how relations manifest when you use Prisma Client to get, create, and update records.
The following query creates a User record and two connected Post records:
const userAndPosts = await prisma.user.create({
data: {
posts: {
create: [
{ title: "Prisma Day 2020" }, // Populates authorId with user's id
{ title: "How to write a Prisma schema" }, // Populates authorId with user's id
],
},
},
});
In the underlying database, this query:
User with an auto-generated id (for example, 20)Post records and sets the authorId of both records to 20The following query retrieves a User by id and includes any related Post records:
const getAuthor = await prisma.user.findUnique({
where: {
id: "20",
},
include: {
posts: true, // All posts where authorId == 20 // [!code highlight]
},
});
In the underlying database, this query:
User record with an id of 20Post records with an authorId of 20The following query associates an existing Post record with an existing User record:
const updateAuthor = await prisma.user.update({
where: {
id: 20,
},
data: {
posts: {
connect: {
id: 4,
},
},
},
});
In the underlying database, this query uses a nested connect query to link the post with an id of 4 to the user with an id of 20. The query does this with the following steps:
id of 20.authorID foreign key to 20. This links the post with an id of 4 to the user with an id of 20.In this query, the current value of authorID does not matter. The query changes authorID to 20, no matter its current value.
There are three different types (or cardinalities) of relations in Prisma ORM:
The following Prisma schema includes every type of relation:
User ↔ ProfileUser ↔ PostPost ↔ Categorymodel User {
id Int @id @default(autoincrement())
posts Post[]
profile Profile?
}
model Profile {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id])
userId Int @unique // relation scalar field (used in the `@relation` attribute above)
}
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)
categories Category[]
}
model Category {
id Int @id @default(autoincrement())
posts Post[]
}
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
posts Post[]
profile Profile?
}
model Profile {
id String @id @default(auto()) @map("_id") @db.ObjectId
user User @relation(fields: [userId], references: [id])
userId String @unique @db.ObjectId // relation scalar field (used in the `@relation` attribute above)
}
model Post {
id String @id @default(auto()) @map("_id") @db.ObjectId
author User @relation(fields: [authorId], references: [id])
authorId String @db.ObjectId // relation scalar field (used in the `@relation` attribute above)
categories Category[] @relation(fields: [categoryIds], references: [id])
categoryIds String[] @db.ObjectId
}
model Category {
id String @id @default(auto()) @map("_id") @db.ObjectId
posts Post[] @relation(fields: [postIds], references: [id])
postIds String[] @db.ObjectId
}
:::info
This schema is the same as the example data model but has all scalar fields removed (except for the required relation scalar fields) so you can focus on the relation fields.
:::
:::info
This example uses implicit many-to-many relations. These relations do not require the @relation attribute unless you need to disambiguate relations.
:::
Notice that the syntax is slightly different between relational databases and MongoDB - particularly for many-to-many relations.
For relational databases, the following entity relationship diagram represents the database that corresponds to the sample Prisma schema:
For MongoDB, Prisma ORM uses a normalized data model design, which means that documents reference each other by ID in a similar way to relational databases. See the MongoDB section for more details.
Many-to-many relations in relational databases can be modelled in two ways:
Implicit many-to-many relations require both models to have a single @id. Be aware of the following:
@unique in place of an @idTo use either of these features, you must set up an explicit many-to-many instead.
The implicit many-to-many relation still manifests in a relation table in the underlying database. However, Prisma ORM manages this relation table.
If you use an implicit many-to-many relation instead of an explicit one, it makes the Prisma Client API simpler (because, for example, you have one fewer level of nesting inside of nested writes).
If you're not using Prisma Migrate but obtain your data model from introspection, you can still make use of implicit many-to-many relations by following Prisma ORM's conventions for relation tables.
Relation fields are fields on a Prisma model that do not have a scalar type. Instead, their type is another model.
Every relation must have exactly two relation fields, one on each model. In the case of one-to-one and one-to-many relations, an additional relation scalar field is required which gets linked by one of the two relation fields in the @relation attribute. This relation scalar field is the direct representation of the foreign key in the underlying database.
model User {
id Int @id @default(autoincrement())
email String @unique
role Role @default(USER)
posts Post[] // relation field (defined only at the Prisma ORM level)
}
model Post {
id Int @id @default(autoincrement())
title String
author User @relation(fields: [authorId], references: [id]) // relation field (uses the relation scalar field `authorId` below)
authorId Int // relation scalar field (used in the `@relation` attribute above)
}
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
email String @unique
role Role @default(USER)
posts Post[] // relation field (defined only at the Prisma ORM level)
}
model Post {
id String @id @default(auto()) @map("_id") @db.ObjectId
title String
author User @relation(fields: [authorId], references: [id]) // relation field (uses the relation scalar field `authorId` below)
authorId String @db.ObjectId // relation scalar field (used in the `@relation` attribute above)
}
Both posts and author are relation fields because their types are not scalar types but other models.
Also note that the annotated relation field author needs to link the relation scalar field authorId on the Post model inside the @relation attribute. The relation scalar field represents the foreign key in the underlying database.
Both the relation fields (i.e. posts and author) are defined purely on a Prisma ORM-level, they don't manifest in the database.
Relations that require one side of the relation to be annotated with the @relation attribute are referred to as annotated relation fields. This includes:
The side of the relation which is annotated with the @relation attribute represents the side that stores the foreign key in the underlying database. The "actual" field that represents the foreign key is required on that side of the relation as well, it's called relation scalar field, and is referenced inside @relation attribute:
author User @relation(fields: [authorId], references: [id])
authorId Int
author User @relation(fields: [authorId], references: [id])
authorId String @db.ObjectId
A scalar field becomes a relation scalar field when it's used in the fields of a @relation attribute.
Because a relation scalar field always belongs to a relation field, the following naming convention is common:
authorauthorId (relation field name + Id)@relation attributeThe @relation attribute can only be applied to the relation fields, not to scalar fields.
The @relation attribute is required when:
Note: Implicit many-to-many relations in relational databases do not require the
@relationattribute.
When you define two relations between the same two models, you need to add the name argument in the @relation attribute to disambiguate them. As an example for why that's needed, consider the following models:
// NOTE: This schema is intentionally incorrect. See below for a working solution.
model User {
id Int @id @default(autoincrement())
name String?
writtenPosts Post[] // [!code highlight]
pinnedPost Post? // [!code highlight]
}
model Post {
id Int @id @default(autoincrement())
title String?
author User @relation(fields: [authorId], references: [id]) // [!code highlight]
authorId Int
pinnedBy User? @relation(fields: [pinnedById], references: [id]) // [!code highlight]
pinnedById Int?
}
// NOTE: This schema is intentionally incorrect. See below for a working solution.
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String?
writtenPosts Post[] // [!code highlight]
pinnedPost Post? // [!code highlight]
}
model Post {
id String @id @default(auto()) @map("_id") @db.ObjectId
title String?
author User @relation(fields: [authorId], references: [id]) // [!code highlight]
authorId String @db.ObjectId
pinnedBy User? @relation(fields: [pinnedById], references: [id]) // [!code highlight]
pinnedById String? @db.ObjectId
}
In that case, the relations are ambiguous, there are four different ways to interpret them:
User.writtenPosts ↔ Post.author + Post.authorIdUser.writtenPosts ↔ Post.pinnedBy + Post.pinnedByIdUser.pinnedPost ↔ Post.author + Post.authorIdUser.pinnedPost ↔ Post.pinnedBy + Post.pinnedByIdTo disambiguate these relations, you need to annotate the relation fields with the @relation attribute and provide the name argument. You can set any name (except for the empty string ""), but it must be the same on both sides of the relation:
model User {
id Int @id @default(autoincrement())
name String?
writtenPosts Post[] @relation("WrittenPosts") // [!code highlight]
pinnedPost Post? @relation("PinnedPost") // [!code highlight]
}
model Post {
id Int @id @default(autoincrement())
title String?
author User @relation("WrittenPosts", fields: [authorId], references: [id]) // [!code highlight]
authorId Int
pinnedBy User? @relation("PinnedPost", fields: [pinnedById], references: [id]) // [!code highlight]
pinnedById Int? @unique
}
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String?
writtenPosts Post[] @relation("WrittenPosts") // [!code highlight]
pinnedPost Post? @relation("PinnedPost") // [!code highlight]
}
model Post {
id String @id @default(auto()) @map("_id") @db.ObjectId
title String?
author User @relation("WrittenPosts", fields: [authorId], references: [id]) // [!code highlight]
authorId String @db.ObjectId
pinnedBy User? @relation("PinnedPost", fields: [pinnedById], references: [id]) // [!code highlight]
pinnedById String? @unique @db.ObjectId
}