Import
Defining Relations
relations()
Define relationships between tables:Table
required
The table to define relations for
function
required
Function that receives helpers and returns relation definitions
Relation Helpers
Therelations function provides two helper functions:
one()
Define a one-to-one or many-to-one relationship:Table
required
The related table
object
Optional configuration for the relation
Column[]
Foreign key columns in the current table
Column[]
Referenced columns in the related table
string
Optional name to disambiguate multiple relations between the same tables
many()
Define a one-to-many relationship:Table
required
The related table
object
Optional configuration
string
Optional name to disambiguate multiple relations
Relation Types
One-to-One
One-to-Many
Many-to-Many
Implemented using a junction table:Querying Relations
Basic Relational Query
Nested Relations
Filtering Related Data
Selecting Specific Columns
Using extras
Add computed fields to the result:Query Configuration
DBQueryConfig Type
The configuration object for relational queries:object
Select specific columns from the table
object
Include related data
SQL | function
Filter conditions for the query
Column | SQL | function | array
Order the results
number | Placeholder
Limit the number of results
number | Placeholder
Offset for pagination (only available on root queries)
object | function
Additional computed fields
Relational Query Methods
findMany()
Find multiple records with relations:findFirst()
Find the first matching record:Disambiguating Relations
When you have multiple relations between the same tables, userelationName:
Operators in Relational Queries
The following operators are available inwhere and orderBy functions:
Comparison Operators
Logical Operators
Pattern Matching
Array Operators
Order By Operators
Best Practices
- Define both sides: Always define relations on both tables for full type safety
- Use relationName: Disambiguate multiple relations between the same tables
- Select wisely: Only select columns you need to improve performance
- Limit nested queries: Deep nesting can impact performance
- Use where clauses: Filter related data to reduce payload size
Related APIs
- Query Builder - Alternative query syntax
- Schema Definition - Define table schemas
- SQL - Use raw SQL in queries