Set Up Prisma Database Seeding
Beginnerv1.0.0
Create idempotent database seed scripts with Prisma — development data, test fixtures, production reference data, and factory patterns for realistic test data.
Content
Overview
Database seeding populates your database with initial data for development, testing, or production reference data. Prisma runs seed scripts automatically after prisma migrate dev and on demand with prisma db seed.
Why This Matters
- -Development — start coding with realistic data immediately
- -Testing — consistent test fixtures for reliable tests
- -Onboarding — new developers get a populated database instantly
- -Production — seed reference data (categories, roles, settings)
How It Works
Step 1: Configure Seed Script
Step 2: Create Seed Script
Step 3: Factory Pattern for Test Data
Step 4: Run Seed
Best Practices
- -Use
upsertfor idempotent seeding (safe to run multiple times) - -Separate development seed data from production reference data
- -Use factories for generating test data in test suites
- -Always handle disconnection in finally/catch blocks
- -Never seed production credentials — use environment-specific seed files
- -Keep seed data realistic to catch edge cases early
Common Mistakes
- -Using
createinstead ofupsert(fails on re-run) - -Seeding real passwords or API keys
- -Not disconnecting PrismaClient after seeding
- -Seed script with hardcoded IDs (conflicts with auto-generated IDs)
- -Missing the prisma.seed config in package.json
FAQ
Discussion
Loading comments...