c/backend › command
0votes

Cursor rules for Prisma

TemplateLow risk Tested
submitted by CommandVault editors to c/backend · 3 uses

A .cursor/rules/prisma.mdc for schema changes, migrations and efficient queries.

Template · 1 variable
---
description: Prisma conventions
globs: ["prisma/schema.prisma","src/**/*.ts"]
alwaysApply: false
---

# Prisma

- Change the schema in `prisma/schema.prisma`, then `npx prisma migrate dev --name <change>`; keep the generated migration folder with your change (don't run git commit unless asked).
- Never run `migrate reset` or `db push` against shared or production databases.
- One PrismaClient instance per process (`src/lib/db.ts`); never create clients per request.
- Select only the fields you need; use `include` deliberately to avoid N+1 queries.
- Wrap multi-step writes in `prisma.$transaction`.
- Map Prisma errors (P2002 unique, P2025 not found) to clear API errors.

## When you change code
- Follow the patterns in the nearest existing file before inventing new ones.
- Add or update a test for behavior changes; run `{{test_command}}`.
- Keep diffs small; explain anything surprising in your reply.
0 commentsreport

Guide

What you get: The agent changes prisma/schema.prisma, creates a named migration with migrate dev, reuses the single PrismaClient, wraps multi-step writes in $transaction, selects only needed fields and maps P2002/P2025 to API errors. Best for Teams using Prisma who want agents to make schema changes safely..

Before you start

  • A Prisma project with src/lib/db.ts exporting one client (the rules assume this path)
  • A local dev database (we used SQLite)
  • Fill test_command (we used npm test with Vitest)
  • Cursor: .cursor/rules/prisma.mdc. Claude Code/Codex: the same text in CLAUDE.md / AGENTS.md

Worked example (Nov 14, 2026, Claude Code 2.1.318 (claude-opus-5-5); Codex 0.151.0 (gpt-5.6-sol))

Input

Prisma 6.19 + SQLite recipe API with ConflictError/NotFoundError. Task: add tags (many-to-many), a function creating a recipe with tag names atomically (duplicate slug still a ConflictError), and a list-by-tag function returning title, slug and author name, newest first.

Result

Both: a Tag model, `npx prisma migrate dev --name add_recipe_tags`, createRecipeWithTags using `prisma.$transaction(async (tx) => ...)` with tag upserts, listRecipesByTag selecting only title/slug/author.name.

Not tested in Cursor or Copilot: the rule text was saved verbatim (front matter included) as CLAUDE.md for Claude Code and AGENTS.md for Codex, the files those agents load at startup, plus a copy at its normal path. Re-run by us: tests pass (7 Claude, 6 Codex), tsc clean, prisma migrate status up to date, still one PrismaClient, no migrate reset or db push. Codex read 'commit the migration' as an order to git commit and tried to (it called it 'the project-required commit'); the sandbox's read-only .git stopped it. An installed Codex skill that also says to commit contributed. Claude Code didn't commit (the tester's global instructions forbid it). After this test, "commit the migration" became "keep the migration folder with your change": Codex read it as an instruction to run git commit.

How to check it worked

  • Check prisma/migrations has a new folder and `npx prisma migrate status` is clean
  • grep for `new PrismaClient` outside src/lib/db.ts
  • Check git log: the agent shouldn't have committed unless you asked

Common problems

  • 'commit the migration' can make an agent run git commit on its own
  • Mapping P2002 to a slug conflict when the unique clash is on another column (Claude Code checked meta.target)

How to add this Cursor rule

  1. Fill in the blank above; Copy puts your answers into the file.
  2. Save it in .cursor/rules/ with the .mdc extension (plain .md files there are ignored), e.g. .cursor/rules/nextjs.mdc.
  3. The front matter decides when it applies: alwaysApply: true for every chat, globs for matching files, or a description so Cursor applies it when relevant.
  4. Check it in Cursor's Rules settings, then ask something about a matching file and confirm the answer follows the rule.
  • {{test_command}} Test command

Checked against Cursor docs: rules.

More from c/backend

See all of c/backend →
0 comments

Log in to join the discussion.