Skip to main content
Drizzle’s relations API enables you to define relationships between tables and query them using an intuitive, type-safe syntax. Relations are defined separately from table schemas and power the relational query API.

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

The relations 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
Implicit relations (inferred from the reverse side):

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

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, use relationName:

Operators in Relational Queries

The following operators are available in where and orderBy functions:

Comparison Operators

Logical Operators

Pattern Matching

Array Operators

Order By Operators

Best Practices

  1. Define both sides: Always define relations on both tables for full type safety
  2. Use relationName: Disambiguate multiple relations between the same tables
  3. Select wisely: Only select columns you need to improve performance
  4. Limit nested queries: Deep nesting can impact performance
  5. Use where clauses: Filter related data to reduce payload size