Skip to main content

What are Seeders?

Seeders are scripts that populate your database with sample or initial data. They’re perfect for setting up test data, default values, or demo content.
Real-world analogy: Seeders are like filling a new garden with plants. After you’ve prepared the soil (migrations), you plant seeds (seeders) to grow your garden with the plants you want.

Creating Seeders

Using the CLI

Basic Seeder Structure

Running Seeders

Basic Commands

Seeder Patterns

Simple Data Seeding

Using Model Methods

Conditional Seeding

Environment-Specific Seeding

Advanced Seeding Patterns

Seeding with Relationships

Bulk Seeding with Faker

Many-to-Many Seeding

Seeder Dependencies

Ordered Seeding

Create a master seeder to control order:

Reference Data Seeding

Production Seeding

Configuration Data

Default Admin User

Testing with Seeders

Test Database Seeding

Performance Optimization

Batch Inserts

Transaction Usage

Best Practices

1. Clear Data First

2. Use Meaningful Data

3. Handle Dependencies

4. Environment Awareness

Troubleshooting

Problem: Cannot insert due to foreign key constraintSolutions:
  • Run seeders in correct order (parent tables first)
  • Check that referenced records exist
  • Use proper foreign key values
Problem: Unique constraint violationSolutions:
  • Clear existing data first: await Model.query().delete()
  • Check for existing records before inserting
  • Use firstOrCreate() or updateOrCreate() for upserts
Problem: Seeder doesn’t executeSolutions:
  • Check seeder file name and location
  • Verify exports.seed function exists
  • Check for syntax errors in seeder file
  • Use --class=SeederName to run specific seeder

Next Steps

Model Factories

Generate realistic test data automatically

Database Connections

Manage multiple database connections