Lookup by ID or unique field
Fetch one account by its primary key or unique email address.
Prisma · Module
findUnique() retrieves one record by ID, a unique field, or a compound unique key. It returns the matching record or null when no record exists, and supports selecting or including related data.
Call findUnique() on a model delegate with a where object containing an ID, unique scalar field, or compound unique key. It returns one matching record, optionally shaped with select or include, or null when no record matches; use findUniqueOrThrow() when absence should raise an error.
prisma.model.findUnique({ where, select?, include? })| Name | Type | Description |
|---|---|---|
args | UserFindUniqueArgs | An object containing a unique where filter and optional payload-shaping options such as select or include. |
A single model record matching the unique filter, or null when no record is found.
findOne API in Prisma 2.12.0.findUnique().findUniqueOrThrow(); that variant throws a PrismaClientKnownRequestError instead.const user = await prisma.user.findUnique({
where: { email: "alice@prisma.io" },
});
if (user) {
console.log(user.id, user.email);
}Looks up one user by its unique email field and handles the null result when no matching record exists.
Fetch one account by its primary key or unique email address.
Retrieve one record using the combined values of a compound unique constraint.
Return a user together with selected related records in one payload.
Traverse a to-one record into related data using chained relation calls.
findUnique() query?findUnique() retrieves one record by ID or by a field marked unique. Its where filter can also use a compound unique key, and the result may be null if no record matches.
Follow-up: How would you make absence exceptional? Use findUniqueOrThrow(), which retrieves the record in the same way but throws a PrismaClientKnownRequestError when none is found.
A compound unique constraint is represented in where by its generated compound key, whose value is an object containing each constrained field. This lets the query identify one record using the combined unique values.
Follow-up: Can a compound unique key be used with arbitrary query methods? No; the documentation limits compound ID and compound unique constraint filters to unique queries such as findUnique() and findUniqueOrThrow(), as well as update, delete, and upsert operations.
Use select to choose returned scalar fields and nested relation fields, or include to include relations. The result can also be followed with the fluent API to fetch related data when the preceding call returns a single object.
Follow-up: What constraint applies to fluent chaining? Each preceding call must return only one object, such as a findUnique() result or a to-one relation.
findUniqueOrThrow()findFirst()findMany()selectincludecompound unique constraintsfluent API