Prisma (Relational)
Purpose
Express the approved relational schema through Prisma and use its client well: schema-as-source, generated types, disciplined queries, and a clear line where raw SQL takes over. The schema design comes from relational-schema-design; this skill is its Prisma expression.
When to Use
- After Prisma + a relational DB were approved (
database-selection).
- Not for schema design decisions (upstream) or Drizzle/Mongoose projects.
Inputs
- Approved schema design (entities, constraints, types, relations).
- Migration/environment workflow expectations (
database-migrations).
Discovery Questions
- Does the schema use features Prisma models natively (enums, composite uniques, referential actions) vs ones needing raw SQL in migrations (partial indexes, CHECK constraints, triggers)?
- Where do generated types flow (service layer boundaries —
../../backend/backend-api-architecture says entities ≠ DTOs)?
- What query hot paths exist (drives select/include discipline)?
Responsibilities
- Express the design in
schema.prisma: types, relations with explicit referential actions (onDelete), @@unique/@@index, enums — mapping every constraint the design demands; what Prisma's DSL can't express (CHECKs, partial indexes) goes into migration SQL deliberately, not dropped.
- Own the migration workflow with
database-migrations: migrate dev in development, generated SQL reviewed like code, migrate deploy in environments; no db push beyond throwaway prototyping; drift detection in CI.
- Set client usage patterns:
select/include scoped to need — no default full-entity fetches leaking through APIs (../../backend/rest-api-design payload discipline);
- relation loading strategy per hot path — avoid N+1 (batched
include, or restructured queries);
$transaction for multi-write invariants (transactions owns the boundaries);
- pagination via cursor patterns on stable keys;
- singleton client with pool sizing matched to deployment (
database-performance).
- Define the raw-SQL escape hatch policy:
$queryRaw (parameterized only — database-security) for reporting/bulk/window queries where the client abstraction fights; typed and reviewed.
- Keep generated client/types out of domain contracts: repository/service boundary maps Prisma models → domain/DTO shapes.
Required Workflow
- Translate the approved design into
schema.prisma; list what needs raw-SQL migration additions.
- Wire the migration workflow (dev/deploy/CI drift check).
- Define client patterns (select scope, N+1 strategy, transactions, pagination).
- Set the raw-SQL policy and pool config.
- Verify constraints landed in SQL (inspect generated migrations) — not just in the DSL.
Decision Rules
- The generated migration SQL is the truth — review it; the DSL is shorthand.
- Any constraint Prisma can't express still ships (raw SQL in the migration) — the design isn't negotiable downward to the tool.
db push never touches shared environments.
- N+1s hide behind lazy convenience: hot paths get explicit query shape review (
database-performance).
Rules
- All raw queries parameterized; string interpolation into SQL is a defect.
- Migration files are immutable once applied beyond dev (
database-migrations).
- Client version and engine pinned; upgrades follow
../../dependency-audit.
Anti-Patterns
- Designing the schema in Prisma DSL ad hoc, skipping the design skill.
- Dropping CHECK/partial-index requirements because the DSL lacks them.
findMany with no select, no take — full rows, unbounded.
- Prisma models serialized straight out of controllers.
- Per-request
new PrismaClient() exhausting connections.
Validation Checklist
Definition of Done
The approved schema fully expressed through Prisma (with deliberate raw-SQL supplements), a reviewed migration workflow, recorded client usage patterns that prevent N+1/overfetch/connection exhaustion, and a parameterized-only raw-SQL policy.
Related Skills
database-selection, relational-schema-design, database-migrations, transactions, indexing, database-performance, database-security, seed-data, ../../backend/backend-api-architecture.
Related Knowledge
../../../knowledge/ (hot paths, environment workflow).
Related References
../../../references/database/prisma/ (patterns, when populated).
Context Loading Guidance
- Requires: approved schema design, migration workflow expectations, hot paths.
- Does not require: re-deciding schema/database choices, app feature code.
- May load:
database-migrations (workflow), transactions (boundaries).
- Stop when: schema expression + workflow + client patterns are recorded.
Token Efficiency Guidance
Work from the schema-design table; don't paste the whole schema.prisma into discussion — reference models by name and show only contested mappings.
1---2name: prisma-relational3description: Use to plan Prisma over PostgreSQL/MySQL — expressing the approved schema in the Prisma schema, migration workflow, client usage patterns (select scope, N+1 avoidance, transactions), and where Prisma's abstractions end (raw SQL escape hatch).4---56# Prisma (Relational)78## Purpose910Express the approved relational schema through Prisma and use its client well: schema-as-source, generated types, disciplined queries, and a clear line where raw SQL takes over. The schema *design* comes from `relational-schema-design`; this skill is its Prisma expression.1112## When to Use1314- After Prisma + a relational DB were approved (`database-selection`).15- **Not** for schema design decisions (upstream) or Drizzle/Mongoose projects.1617## Inputs1819- Approved schema design (entities, constraints, types, relations).20- Migration/environment workflow expectations (`database-migrations`).2122## Discovery Questions2324- Does the schema use features Prisma models natively (enums, composite uniques, referential actions) vs ones needing raw SQL in migrations (partial indexes, CHECK constraints, triggers)?25- Where do generated types flow (service layer boundaries — `../../backend/backend-api-architecture` says entities ≠ DTOs)?26- What query hot paths exist (drives select/include discipline)?2728## Responsibilities2930- Express the design in `schema.prisma`: types, relations with explicit referential actions (`onDelete`), `@@unique`/`@@index`, enums — mapping every constraint the design demands; what Prisma's DSL can't express (CHECKs, partial indexes) goes into migration SQL deliberately, not dropped.31- Own the **migration workflow** with `database-migrations`: `migrate dev` in development, generated SQL **reviewed like code**, `migrate deploy` in environments; no `db push` beyond throwaway prototyping; drift detection in CI.32- Set **client usage patterns**:33 - `select`/`include` scoped to need — no default full-entity fetches leaking through APIs (`../../backend/rest-api-design` payload discipline);34 - relation loading strategy per hot path — avoid N+1 (batched `include`, or restructured queries);35 - `$transaction` for multi-write invariants (`transactions` owns the boundaries);36 - pagination via cursor patterns on stable keys;37 - singleton client with pool sizing matched to deployment (`database-performance`).38- Define the **raw-SQL escape hatch** policy: `$queryRaw` (parameterized only — `database-security`) for reporting/bulk/window queries where the client abstraction fights; typed and reviewed.39- Keep generated client/types out of domain contracts: repository/service boundary maps Prisma models → domain/DTO shapes.4041## Required Workflow42431. Translate the approved design into `schema.prisma`; list what needs raw-SQL migration additions.442. Wire the migration workflow (dev/deploy/CI drift check).453. Define client patterns (select scope, N+1 strategy, transactions, pagination).464. Set the raw-SQL policy and pool config.475. Verify constraints landed in SQL (inspect generated migrations) — not just in the DSL.4849## Decision Rules5051- The generated migration SQL is the truth — review it; the DSL is shorthand.52- Any constraint Prisma can't express still ships (raw SQL in the migration) — the design isn't negotiable downward to the tool.53- `db push` never touches shared environments.54- N+1s hide behind lazy convenience: hot paths get explicit query shape review (`database-performance`).5556## Rules5758- All raw queries parameterized; string interpolation into SQL is a defect.59- Migration files are immutable once applied beyond dev (`database-migrations`).60- Client version and engine pinned; upgrades follow `../../dependency-audit`.6162## Anti-Patterns6364- Designing the schema *in* Prisma DSL ad hoc, skipping the design skill.65- Dropping CHECK/partial-index requirements because the DSL lacks them.66- `findMany` with no `select`, no `take` — full rows, unbounded.67- Prisma models serialized straight out of controllers.68- Per-request `new PrismaClient()` exhausting connections.6970## Validation Checklist7172- [ ] Design fully expressed (DSL + raw-SQL supplements listed).73- [ ] Migration workflow wired; generated SQL reviewed; CI drift check.74- [ ] Client patterns set: select scope, N+1 plan, transactions, pagination.75- [ ] Raw-SQL policy (parameterized, reviewed) recorded.76- [ ] Pool sizing matched to deployment.77- [ ] Models mapped to DTOs at the boundary.7879## Definition of Done8081The approved schema fully expressed through Prisma (with deliberate raw-SQL supplements), a reviewed migration workflow, recorded client usage patterns that prevent N+1/overfetch/connection exhaustion, and a parameterized-only raw-SQL policy.8283## Related Skills8485`database-selection`, `relational-schema-design`, `database-migrations`, `transactions`, `indexing`, `database-performance`, `database-security`, `seed-data`, `../../backend/backend-api-architecture`.8687## Related Knowledge8889`../../../knowledge/` (hot paths, environment workflow).9091## Related References9293`../../../references/database/prisma/` (patterns, when populated).9495## Context Loading Guidance9697- **Requires:** approved schema design, migration workflow expectations, hot paths.98- **Does not require:** re-deciding schema/database choices, app feature code.99- **May load:** `database-migrations` (workflow), `transactions` (boundaries).100- **Stop when:** schema expression + workflow + client patterns are recorded.101102## Token Efficiency Guidance103104Work from the schema-design table; don't paste the whole `schema.prisma` into discussion — reference models by name and show only contested mappings.