Skip to main content

Relationships

Prism supports three relationship types between database models: hasMany, belongsTo, and hasOne. Define them on your models, then load related data with simple queries.

Relationship Types

hasMany

A user has many posts. The foreign key lives on the related table.

belongsTo

A post belongs to a user. The foreign key lives on this table.

hasOne

A user has one profile. Like hasMany but returns a single row.

Defining Relationships

Schema
User Model with Relationships
Post Model

Has Many

Load User's Posts

Belongs To

Load Post's Author

Has One

Load User's Profile

API Example

Build an endpoint that returns users with their posts:
Users with Posts API
For performance-sensitive endpoints, consider using a single JOIN query instead of multiple relationship loads. Relationships are convenient for simple cases; raw SQL with JOINs is better when you need to minimize round trips.