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:- Users - Test user accounts with hashed passwords
- Categories - Meal types and diet types
- Profiles - User nutritional profiles with goals and preferences
- Recipes - Recipe library from JSON data
- Plans - Sample meal plans with daily menus
Running the Seeder
Local Environment
Run the seeder script directly:Docker Environment
Run the seeder inside the backend container: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:Users
Creates test user accounts:
- [email protected] (password: Ruyi1234)
- [email protected] (password: Elias1234)
- [email protected] (password: Cris1234)
backend/app/seeders/users.py:8Categories
Populates meal types and diet types:Meal Types:
- breakfast
- lunch
- dinner
- snack
- high_protein
- low_carb
- vegan
- vegetarian
- low_calorie
- high_fiber
- high_carb
backend/app/seeders/categories.py:8Profiles
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
backend/app/seeders/profiles.pyRecipes
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
backend/app/seeders/recipes.py:7Seeder File Structure
All seeder files are located inbackend/app/seeders/:
Main Seeder Script
Therun_seed.py file orchestrates all seeders:
backend/app/seeders/run_seed.py:9
Individual Seeder Examples
User Seeder
The user seeder creates test accounts with hashed passwords:backend/app/seeders/users.py:8
Category Seeder
The category seeder populates diet and meal types:backend/app/seeders/categories.py:8
Recipe Seeder
The recipe seeder loads from a JSON file:backend/app/seeders/recipes.py:7
Idempotent Seeding
Seeders are designed to be idempotent - running them multiple times won’t create duplicate data:Recipe Data Format
Recipes are stored inbackend/app/data/recipes.json with this structure:
Customizing Seed Data
Modify Test Users
Editbackend/app/seeders/users.py to add or change test accounts:
Add More Categories
Editbackend/app/seeders/categories.py to add meal or diet types:
Add Custom Recipes
Editbackend/app/data/recipes.json to include your own recipes:
Clearing Seeded Data
To remove seeded data and start fresh: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 expectsbackend/app/data/recipes.json to exist. Verify the file path and structure.
Best Practices
Seeding guidelines:
- Always run migrations before seeding
- Use seeders for development and testing only
- Don’t use seed data in production
- Keep seed data realistic but minimal
- Document any custom seed data
- Make seeders idempotent to avoid duplicates
- Test seeders after schema changes
See Also
- Database Schema - Understanding the database structure
- Database Migrations - Managing schema changes
- Backend README - Backend setup guide
