Skip to main content
PostgreSQL, Prisma & Drizzle ORMdatabaseTarget: PostgreSQL 16+ / Prisma 5+ / Drizzle

PostgreSQL, Prisma & Drizzle AI Agent Rules & Cursorrules

Eliminate N+1 database bottlenecks and unsafe table locks in AI-generated code.

Prevent AI agents from writing disastrous database migrations and slow unindexed queries. Enforce compound indexing, non-blocking lock timeouts, and safe ORM joins.

#postgresql cursorrules#prisma cursorrules#drizzle cursor rules#sql agent rules#cursor rules for database

Configuration Workbench

Select your agent format below to preview, copy, or download the configuration file.

Cursor configuration for PostgreSQL and ORMs.
11 lines•0.5 KB
# PostgreSQL & ORM Cursor Rules

You are a Principal Database Administrator and Performance Engineer.

## Schema & Query Invariants
- PostgreSQL: 16+.
- Every foreign key must have an accompanying index.
- Never write N+1 query loops. Always use batching or eager loading with explicit joins.
- Use UUIDv7 or ULID for primary keys to maintain sequential B-tree ordering.
- For large migrations, use 'CREATE INDEX CONCURRENTLY' with strict statement timeouts.
- Never store raw passwords or unhashed tokens. Use Argon2id or bcrypt.

Pro-tip: Place this file directly in the root directory of your PostgreSQL, Prisma & Drizzle ORM project. AI agents like Cursor Composer, Claude Code, and GitHub Copilot will read it automatically on initialization.

Architectural Principles Enforced

When this configuration file is loaded into Cursor or Claude Code, the agent strictly enforces the following design invariants:

Always add compound database indexes to foreign keys and filtered columns.

Never generate N+1 query patterns; use relational joins or batching (`include`, `with`).

Enforce lock timeouts (`SET lock_timeout = '2s'`) on production schema migrations.

Use `CONCURRENTLY` when building indexes on existing PostgreSQL tables with production traffic.

Use ULID or UUIDv7 for distributed identifiers to preserve B-tree index insertion locality.

Installation & Repository Setup

Place in project root as `.cursorrules` or `.cursor/rules/database.mdc`.

1Add `.cursorrules` to your database directory or repository root.
2Verify all migrations with dry-run scripts before applying.

Verified Agent Behavior Examples

Observe how the AI agent responds when prompted under these active rules.

Scenario: Fetching user orders without N+1 query loop
User prompt: “Write a query to list users and their recent 5 orders.”
Agent Behavior: The agent generates an optimized single query with a lateral join or single relational `include` statement, preventing iterative loop queries.

Frequently Asked Questions

Why are UUIDv7 primary keys recommended over UUIDv4?

UUIDv4 is purely random, which causes severe B-tree page fragmentation and slow index insertions on large tables. UUIDv7 embeds a timestamp prefix, ensuring chronological insertion order while retaining global uniqueness.