Skip to main content

Overview

The seeding system populates the database with initial data for development and testing. SmartEat AI includes seeders for users, categories, profiles, recipes, and meal plans.

What Gets Seeded

The seeder system creates the following data:
  1. Users - Test user accounts with hashed passwords
  2. Categories - Meal types and diet types
  3. Profiles - User nutritional profiles with goals and preferences
  4. Recipes - Recipe library from JSON data
  5. Plans - Sample meal plans with daily menus

Running the Seeder

Local Environment

Run the seeder script directly:
Or from the backend directory:

Docker Environment

Run the seeder inside the backend container:
Or with PYTHONPATH explicitly set:
Prerequisites:
  • Database must be running and accessible
  • All migrations must be applied (alembic upgrade head)
  • Environment variables must be configured in .env

Seeder Execution Order

Seeders run in a specific order to respect foreign key constraints:
1

Users

Creates test user accounts:From: backend/app/seeders/users.py:8
2

Categories

Populates meal types and diet types:Meal Types:
  • breakfast
  • lunch
  • dinner
  • snack
Diet Types:
  • high_protein
  • low_carb
  • vegan
  • vegetarian
  • low_calorie
  • high_fiber
  • high_carb
From: backend/app/seeders/categories.py:8
3

Profiles

Creates nutritional profiles for test users with:
  • Physical metrics (height, weight, body type)
  • Goals (lose/maintain/gain weight)
  • Activity levels
  • Macro targets
  • Dietary preferences and restrictions
From: backend/app/seeders/profiles.py
4

Recipes

Loads recipes from backend/app/data/recipes.json including:
  • Recipe names and descriptions
  • Nutritional information (calories, protein, carbs, fat)
  • Meal type associations
  • Diet type compatibility
  • Image URLs and recipe links
  • Ingredients list
From: backend/app/seeders/recipes.py:7
5

Plans

Generates sample meal plans:
  • Weekly meal schedules
  • Daily menus for each day
  • Meal details with assigned recipes
  • Meal timing and status
From: backend/app/seeders/plans.py

Seeder File Structure

All seeder files are located in backend/app/seeders/:

Main Seeder Script

The run_seed.py file orchestrates all seeders:
From: backend/app/seeders/run_seed.py:9

Individual Seeder Examples

User Seeder

The user seeder creates test accounts with hashed passwords:
From: backend/app/seeders/users.py:8

Category Seeder

The category seeder populates diet and meal types:
From: backend/app/seeders/categories.py:8

Recipe Seeder

The recipe seeder loads from a JSON file:
From: backend/app/seeders/recipes.py:7

Idempotent Seeding

Seeders are designed to be idempotent - running them multiple times won’t create duplicate data:
This allows you to safely re-run seeders without clearing the database.

Recipe Data Format

Recipes are stored in backend/app/data/recipes.json with this structure:

Customizing Seed Data

Modify Test Users

Edit backend/app/seeders/users.py to add or change test accounts:

Add More Categories

Edit backend/app/seeders/categories.py to add meal or diet types:

Add Custom Recipes

Edit backend/app/data/recipes.json to include your own recipes:

Clearing Seeded Data

To remove seeded data and start fresh:
1

Downgrade migrations

2

Upgrade migrations

3

Run seeders again

Warning: This will delete ALL data in the database. Only use in development environments.

Troubleshooting

Import Errors

If you get import errors when running seeders:

Database Connection Issues

Ensure your .env file has the correct database URL:

Foreign Key Constraint Errors

Make sure migrations are up to date:

JSON File Not Found

The recipes seeder expects backend/app/data/recipes.json to exist. Verify the file path and structure.

Best Practices

Seeding guidelines:
  1. Always run migrations before seeding
  2. Use seeders for development and testing only
  3. Don’t use seed data in production
  4. Keep seed data realistic but minimal
  5. Document any custom seed data
  6. Make seeders idempotent to avoid duplicates
  7. Test seeders after schema changes

See Also