intermediate

Relations

Model one-to-one, one-to-many, many-to-many, and explicit join tables with predictable loading and referential behavior.

Prisma relations describe how models connect; foreign keys and join tables enforce database shape. Model ownership, cardinality, delete behavior, and loading strategy together.

					model OrderItem {
  orderId String
  order   Order  @relation(fields: [orderId], references: [id], onDelete: Cascade)
}
				

On interviews: one-to-one, one-to-many, many-to-many, explicit join models, referential actions, avoiding accidental N+1 with `include`.

Common pitfalls: implicit many-to-many when relation attributes exist; cascades deleting more than expected; loading entire graphs by default.

The trade-off is ergonomic relation syntax versus explicit join modeling.

Checklist:

  • Choose explicit joins when relation has data.
  • Set referential actions deliberately.
  • Plan loading strategy per endpoint.
  • Index foreign keys used in filters.