One-to-one relations (Prisma ORM v7) (/docs/orm/v7/prisma-schema/data-model/relations/one-to-one-relations)
For the complete Prisma documentation index, see llms.txt. A markdown version of any docs page is available by appending
.mdto its URL.
How to define and work with one-to-one relations in Prisma.
Location: ORM > v7 > Prisma Schema > Data Model > Relations > One-to-one relations
One-to-one (1-1) relations connect at most one record on each side. In this example, User and Profile have a 1-1 relation:
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 // Foreign key with unique constraint
}This expresses:
- A user can have zero or one profile
- A profile must always be connected to exactly one user
You can also reference a non-ID field with @unique:
model Profile {
id Int @id @default(autoincrement())
user User @relation(fields: [userEmail], references: [email])
userEmail String @unique
}Multi-field relations (relational databases only)
Section titled “Multi-field relations (relational databases only)”model User {
firstName String
lastName String
profile Profile?
@@id([firstName, lastName])
}
model Profile {
id Int @id @default(autoincrement())
user User @relation(fields: [userFirstName, userLastName], references: [firstName, lastName])
userFirstName String
userLastName String
@@unique([userFirstName, userLastName])
}1-1 in the database
Section titled “1-1 in the database”In SQL, a 1-1 relation requires a UNIQUE constraint on the foreign key. Without this, it becomes a 1-n relation.
For MongoDB, documents reference each other by ID:
// User
{ "_id": { "$oid": "60d58e130011041800d209e1" }, "name": "Bob" }
// Profile
{ "_id": "...", "bio": "I like drawing.", "userId": { "$oid": "60d58e130011041800d209e1" } }Required and optional 1-1 relation fields
Section titled “Required and optional 1-1 relation fields”The side without a relation scalar must be optional:
model User {
id Int @id @default(autoincrement())
profile Profile? // No relation scalar - must be optional
}The side with a relation scalar can be required or optional:
Mandatory 1-1 (cannot create User without Profile):
model User {
id Int @id @default(autoincrement())
profile Profile @relation(fields: [profileId], references: [id])
profileId Int @unique
}Optional 1-1 (can create User without Profile):
model User {
id Int @id @default(autoincrement())
profile Profile? @relation(fields: [profileId], references: [id])
profileId Int? @unique
}Choosing which side stores the foreign key
Section titled “Choosing which side stores the foreign key”In 1-1 relations, you can choose which side holds the @relation attribute and foreign key. Both approaches are valid:
Option 1: Foreign key on Profile
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
}Option 2: Foreign key on User
model User {
id Int @id @default(autoincrement())
profile Profile? @relation(fields: [profileId], references: [id])
profileId Int? @unique
}
model Profile {
id Int @id @default(autoincrement())
user User?
}Related pages
Section titled “Related pages”Many-to-many relations: How to define and work with many-to-many relations in Prisma.One-to-many relations: How to define and work with one-to-many relations in Prisma.Referential actions: Referential actions let you define the update and delete behavior of related models on the database levelRelation mode: Manage relations between records with relation modes in PrismaSelf-relations: How to define and work with self-relations in Prisma.