Skip to main content
Prisma Documentation Docs

Search documentation

Type to search this documentation.

On this pageOverview

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 .md to 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:

prisma
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:

prisma
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)”
prisma
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])
}

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:

JSON
// User
{ "_id": { "$oid": "60d58e130011041800d209e1" }, "name": "Bob" }
// Profile
{ "_id": "...", "bio": "I like drawing.", "userId": { "$oid": "60d58e130011041800d209e1" } }

The side without a relation scalar must be optional:

prisma
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):

prisma
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):

prisma
model User {
  id        Int      @id @default(autoincrement())
  profile   Profile? @relation(fields: [profileId], references: [id])
  profileId Int?     @unique
}

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

prisma
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

prisma
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?
}
  • 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 level
  • Relation mode: Manage relations between records with relation modes in Prisma
  • Self-relations: How to define and work with self-relations in Prisma.
Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu