Relationships guide
Create relational mock data without orphaned records
Mock API Studio connects flat parent and child resources through configured keys. The local runtime assigns child foreign keys from real parent records, exposes nested routes, checks that the parent exists, and prevents deletion when it would leave orphaned children.
Define the parent and child resources
Create /clients as the parent resource and /products as the child. Keep each response flat and give the child a primitive foreign key. Both endpoints need their own MockApiStudioResponse binding.
// /clients
interface Client {
id: string;
name: string;
}
type MockApiStudioResponse = Client[];
// /products
interface Product {
id: string;
clientId: string;
name: string;
price: number;
}
type MockApiStudioResponse = Product[];Configure the relationship
In the project relationship editor, select /clients as the parent and /products as the child. Use id as the parent key and clientId as the child foreign key. Choose the relationship type that matches the contract.
At generation time, parent records are created first. The runtime collects their key values and distributes those values across child records, so every generated clientId points to an existing client.
Use nested routes
A nested request first resolves and validates the parent. An unknown client returns 404. For an existing client, the runtime filters products whose clientId equals the parent id.
GET /clients/client-001/products
{
"data": [
{
"id": "product-001",
"clientId": "client-001",
"name": "Mechanical Keyboard",
"price": 89.99
}
],
"meta": { "page": 1, "limit": 25, "total": 1, "totalPages": 1 }
}Create children and protect parents
When creating through the nested child route, the parent must exist. The runtime applies the parent key to the child foreign-key field rather than trusting a conflicting value in the request body.
Deleting a parent that still has children returns 409 Conflict. Remove the child records first, then delete the parent. This restrictive behavior protects referential integrity; automatic cascade deletion is not currently performed.
POST /clients/client-001/products
content-type: application/json
{ "name": "Mechanical Keyboard", "price": 89.99 }
DELETE /clients/client-001
# 409 while related /products records still existCurrent relationship boundaries
Relationships join top-level primitive fields between registered resources. They do not embed child arrays into parent JSON or model an external database. Relationship configuration and endpoint definitions are synchronized together, and changing them may trigger local data regeneration on the next CLI start.
Related questions
How does generated data avoid orphaned records?
The runtime generates parent records first and assigns child foreign keys from the actual generated parent-key values.
What happens when a nested route names a missing parent?
The runtime returns HTTP 404 before querying or creating child records.
Does deleting a parent cascade to its children?
No. If child records still reference the parent, deletion returns HTTP 409. Delete those child records first.
Keep exploring
Last updated September 17, 2026. Product behavior described here is verified against the current Mock API Studio runtime.