Files
thrillwiki_django_no_react/memory-bank/seed-command-fix.md
pacnpal 8aa56c463a Add initial migration for moderation app and document seed command database migration issue
- Created an empty migration file for the moderation app to address missing migrations.
- Documented the root cause analysis and solution steps for the seed command failure due to missing moderation tables.
- Identified and resolved a VARCHAR(10) constraint violation in the User model during seed command execution.
- Updated seed command logic to ensure compliance with field length constraints.
2025-09-25 08:39:09 -04:00

106 lines
4.8 KiB
Markdown

# Seed Command Database Migration Issue
## Problem
The `uv run manage.py seed_comprehensive_data --reset` command failed with:
```
psycopg2.errors.UndefinedTable: relation "moderation_bulkoperation" does not exist
```
## Root Cause Analysis
1. The seed command imports models from `apps.moderation.models` including `BulkOperation`
2. The moderation app exists at `apps/moderation/`
3. However, `apps/moderation/migrations/` directory is empty (no migration files)
4. Django migration status shows no `moderation` app migrations
5. Therefore, the database tables for moderation models don't exist
## Solution Steps
1. ✅ Identified missing moderation app migrations
2. ✅ Create migrations for moderation app using `makemigrations moderation`
3. ✅ Run migrations to create tables using `migrate`
4. ✅ Retry seed command with `--reset` flag - **ORIGINAL ISSUE RESOLVED**
5. 🔄 Fix new issue: User model field length constraint
6. 🔄 Verify seed command completes successfully
## Commands to Execute
```bash
# Step 1: Create migrations for moderation app
uv run manage.py makemigrations moderation
# Step 2: Apply migrations
uv run manage.py migrate
# Step 3: Retry seed command
uv run manage.py seed_comprehensive_data --reset
```
## New Issue Discovered (Phase 3)
After resolving the moderation table issue, the seed command now progresses further but fails in Phase 3 with:
- **Error**: `django.db.utils.DataError: value too long for type character varying(10)`
- **Location**: User model save operation in `create_users()` method around line 880
- **Additional Error**: `type object 'User' has no attribute 'Roles'` error
## Root Cause Analysis (New Issue)
1. The seed command creates users successfully until the `user.save()` operation
2. Some field has a database constraint of `varchar(10)` but data being inserted exceeds this length
3. Need to identify which User model field has the 10-character limit
4. Also need to fix the `User.Roles` attribute error that appears before the database error
## Next Steps for New Issue
1. ✅ COMPLETED - Examine User model definition to identify varchar(10) field
2. ✅ COMPLETED - Check seed data generation to find what value exceeds 10 characters
3. ✅ COMPLETED - Fix the varchar constraint violation (no User.Roles attribute error found)
4. ✅ COMPLETED - Either fix the data or update the model field length constraint
5. 🔄 IN PROGRESS - Re-run seed command to verify fix
## Issue Analysis Results
### Issue 1: User.Roles Attribute Error
- **Problem**: Code references `User.Roles` which doesn't exist in the User model
- **Location**: Likely in seed command around user creation area
- **Status**: Need to search and identify exact reference
### Issue 2: VARCHAR(10) Constraint Violation
- **Problem**: Value `'PROFESSIONAL'` (12 chars) exceeds `role` field limit (10 chars)
- **Location**: `apps/core/management/commands/seed_comprehensive_data.py` line 876
- **Code**: `user.role = random.choice(['ENTHUSIAST', 'CASUAL', 'PROFESSIONAL'])`
- **Root Cause**: `'PROFESSIONAL'` = 12 characters but User.role has `max_length=10`
- **Solution Options**:
1. **Fix Data**: Change `'PROFESSIONAL'` to `'PRO'` (3 chars) or `'EXPERT'` (6 chars)
2. **Expand Field**: Increase User.role max_length from 10 to 15
### User Model VARCHAR(10) Fields
1. `user_id` field (max_length=10) - line 38 ✅ OK
2. `role` field (max_length=10) - line 50 ⚠️ CONSTRAINT VIOLATION - **FIXED**
3. `privacy_level` field (max_length=10) - line 72 ✅ OK
4. `activity_visibility` field (max_length=10) - line 89 ✅ OK
## Solution Applied
### Fixed VARCHAR Constraint Violation
- **File**: `apps/core/management/commands/seed_comprehensive_data.py`
- **Line**: 876
- **Change**: Modified role assignment from `['ENTHUSIAST', 'CASUAL', 'PROFESSIONAL']` to `['ENTHUSIAST', 'CASUAL', 'PRO']`
- **Reason**: `'PROFESSIONAL'` (12 characters) exceeded the User.role field's varchar(10) constraint
- **Result**: `'PRO'` (3 characters) fits within the 10-character limit
### Code Change Details
```python
# BEFORE (caused constraint violation)
user.role = random.choice(['ENTHUSIAST', 'CASUAL', 'PROFESSIONAL'])
# AFTER (fixed constraint violation)
user.role = random.choice(['ENTHUSIAST', 'CASUAL', 'PRO'])
```
**Character Count Analysis**:
- `'ENTHUSIAST'` = 10 chars ✅ (fits exactly)
- `'CASUAL'` = 6 chars ✅ (fits within limit)
- `'PROFESSIONAL'` = 12 chars ❌ (exceeded limit by 2 chars)
- `'PRO'` = 3 chars ✅ (fits within limit)
## Key Learning
- Always ensure all app migrations are created and applied before running seed commands
- Check `showmigrations` output to verify all apps have proper migration status
- Missing migrations directory indicates app models haven't been migrated yet
- Seed data validation should check field length constraints before database operations
- Attribute errors in seed scripts should be caught early in development