Working with Relations
Prisma Client makes it easy to work with related data through theinclude and select options.
Including Relations
Theinclude option loads related records alongside the main record:
Basic Include
Nested Includes
Filtering Included Relations
Selecting Fields
Theselect option chooses specific fields to return:
Basic Select
Select with Relations
Useselect to include specific fields from relations:
Select vs Include
Key difference:include: Returns all model fields + specified relationsselect: Returns only specified fields (must explicitly include each)
select and include at the same level:
Relation Types
One-to-Many
One-to-One
Many-to-Many
Fluent API
The fluent API allows traversing relations through method chaining:/home/daytona/workspace/source/packages/client/src/runtime/core/model/applyFluent.ts
Nested Writes
Create with Relations
Update with Relations
Connect and Disconnect
Relation Count
Get the count of related records without loading them:Relation Filters
Filter parent records based on related data:Relation Loading Performance
N+1 Problem
Select Only What You Need
Limit Related Records
Next Steps
Filtering & Sorting
Advanced where clauses and orderBy
Aggregations
Count and aggregate related data