seed Command

Populate database tables with test data using the Akron CLI across SQLite, MySQL, PostgreSQL, and MongoDB.

Overview

The seed command populates your database tables with test or initial data. Perfect for development, testing, and setting up demo environments with realistic data.

Basic Syntax

akron seed <table_name> --db <database_url> --data <data_json>

Parameters

table_name

Type: str (required)

Name of the table to populate with data.

--db

Type: str (required)

Database connection URL.

--data

Type: JSON array (required)

Array of objects to insert into the table.

--file

Type: str (optional)

Path to JSON file containing seed data (alternative to --data).

Examples

Basic Seeding

1# Seed users table with inline data
2akron seed users --db "sqlite:///app.db" --data '[
3 {"name": "John Doe", "email": "john@example.com"},
4 {"name": "Jane Smith", "email": "jane@example.com"}
5]'
Expected Output
✓ Seeded 2 records into 'users' table
✓ Data inserted successfully

Seeding from File

1# Create seed data file
2echo '[
3 {"name": "Product 1", "price": 29.99, "category": "Electronics"},
4 {"name": "Product 2", "price": 49.99, "category": "Books"},
5 {"name": "Product 3", "price": 19.99, "category": "Clothing"}
6]' > products_seed.json
7
8# Seed from file
9akron seed products --db "mysql://user:pass@host:3306/store" --file products_seed.json
Expected Output
✓ Loaded seed data from products_seed.json
✓ Seeded 3 records into 'products' table

Complex Data Types

1# Seed with various data types
2akron seed orders --db "postgres://user:pass@host:5432/shop" --data '[
3 {
4 "id": 1,
5 "customer_id": 101,
6 "total": 99.99,
7 "status": "completed",
8 "items": ["item1", "item2"],
9 "created_at": "2024-01-15T10:30:00Z"
10 },
11 {
12 "id": 2,
13 "customer_id": 102,
14 "total": 149.50,
15 "status": "pending",
16 "items": ["item3"],
17 "created_at": "2024-01-16T14:20:00Z"
18 }
19]'
Expected Output
✓ Seeded 2 records into 'orders' table
✓ Complex data types handled automatically

MongoDB Documents

1# Seed MongoDB collection
2akron seed user_profiles --db "mongodb://localhost:27017/social" --data '[
3 {
4 "username": "alice",
5 "profile": {
6 "firstName": "Alice",
7 "lastName": "Johnson",
8 "age": 28
9 },
10 "tags": ["developer", "react", "nodejs"],
11 "preferences": {
12 "theme": "dark",
13 "notifications": true
14 }
15 }
16]'
Expected Output
✓ Seeded 1 document into 'user_profiles' collection
✓ Nested objects and arrays preserved

Seed Data Patterns

User Data Pattern

1# users_seed.json
2[
3 {
4 "id": 1,
5 "username": "admin",
6 "email": "admin@example.com",
7 "password_hash": "hashed_password_here",
8 "role": "admin",
9 "is_active": true,
10 "created_at": "2024-01-01T00:00:00Z"
11 },
12 {
13 "id": 2,
14 "username": "testuser",
15 "email": "test@example.com",
16 "password_hash": "another_hash",
17 "role": "user",
18 "is_active": true,
19 "created_at": "2024-01-02T00:00:00Z"
20 }
21]

E-commerce Products

1# products_seed.json
2[
3 {
4 "sku": "LAPTOP001",
5 "name": "Gaming Laptop",
6 "description": "High-performance gaming laptop",
7 "price": 1299.99,
8 "cost": 800.00,
9 "category": "Electronics",
10 "in_stock": true,
11 "stock_quantity": 50,
12 "tags": ["gaming", "laptop", "electronics"]
13 },
14 {
15 "sku": "BOOK001",
16 "name": "Python Programming",
17 "description": "Learn Python programming",
18 "price": 39.99,
19 "cost": 15.00,
20 "category": "Books",
21 "in_stock": true,
22 "stock_quantity": 100,
23 "tags": ["programming", "python", "education"]
24 }
25]

Advanced Features

Batch Processing

Large datasets are automatically processed in batches for optimal performance:

1# Seed large dataset (automatically batched)
2akron seed large_table --db "postgres://user:pass@host:5432/db" --file large_dataset.json

Data Validation

Seed data is validated against table schema before insertion, preventing type errors.

Duplicate Handling

Use unique constraints or primary keys to handle duplicate data appropriately.

Error Handling

Common Errors

  • Table does not exist: Create table first with create-table
  • Invalid JSON format: Validate JSON syntax in data or file
  • Schema mismatch: Ensure data matches table column types
  • Constraint violations: Check for duplicate keys or foreign key constraints

Troubleshooting

1# Validate table schema first
2akron inspect-schema --db "sqlite:///app.db"
3
4# Test with small dataset first
5akron seed users --db "sqlite:///app.db" --data '[{"name": "Test User"}]'
6
7# Check for existing data
8akron raw-sql --db "sqlite:///app.db" --query "SELECT COUNT(*) FROM users"

Related Commands