One-to-One
A one-to-one relation links two rows that stand for the same thing, such as a user and its profile: at most one row on each side may point at the other. In the database it is an ordinary foreign key column with a unique constraint.
Definition
Declare user: User @unique on the owning side to make the generated userId unique. The inverse is optional: profile?: Profile @via(user). With an explicit foreign key, uniqueness may be declared on the bound scalar column; relation-level @unique also makes that column unique.
Everything else follows the one-to-many rules: the column name is the relation name plus the capitalized target key, and the type and length are copied from the target's primary key.
Example
model User {
id: int @id @auto
email: string @unique
profile?: Profile @via(user)
}
model Profile {
id: int @id @auto
bio: string = ""
user: User @unique
}2
3
4
5
6
7
8
9
10
11
The foreign key lives on the dependent side: a user exists before their profile, so Profile holds userId. The relation reads like any other:
const user = await db.user.findUniqueOrThrow({
where: { id: 1 },
include: { profile: true },
});2
3
4
Notes
- The owning side may also be optional:
user?: User @uniquelets a profile exist without a user for a while, and the unique constraint still prevents two profiles from sharing the same user. - Foreign-key uniqueness determines the inverse cardinality; without it, several profiles may reference the same user.
- An owning foreign key cannot reference a composite primary key; see Surrogate Keys Instead of Composite Keys.
Next Steps
- Many-to-Many: when both sides own a collection of the other
- Foreign Key Fields: declare the unique column yourself