mirror of
https://github.com/pacnpal/thrillwiki_django_no_react.git
synced 2025-12-20 11:11:10 -05:00
- 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.
4.8 KiB
4.8 KiB
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
- The seed command imports models from
apps.moderation.modelsincludingBulkOperation - The moderation app exists at
apps/moderation/ - However,
apps/moderation/migrations/directory is empty (no migration files) - Django migration status shows no
moderationapp migrations - Therefore, the database tables for moderation models don't exist
Solution Steps
- ✅ Identified missing moderation app migrations
- ✅ Create migrations for moderation app using
makemigrations moderation - ✅ Run migrations to create tables using
migrate - ✅ Retry seed command with
--resetflag - ORIGINAL ISSUE RESOLVED - 🔄 Fix new issue: User model field length constraint
- 🔄 Verify seed command completes successfully
Commands to Execute
# 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)
- The seed command creates users successfully until the
user.save()operation - Some field has a database constraint of
varchar(10)but data being inserted exceeds this length - Need to identify which User model field has the 10-character limit
- Also need to fix the
User.Rolesattribute error that appears before the database error
Next Steps for New Issue
- ✅ COMPLETED - Examine User model definition to identify varchar(10) field
- ✅ COMPLETED - Check seed data generation to find what value exceeds 10 characters
- ✅ COMPLETED - Fix the varchar constraint violation (no User.Roles attribute error found)
- ✅ COMPLETED - Either fix the data or update the model field length constraint
- 🔄 IN PROGRESS - Re-run seed command to verify fix
Issue Analysis Results
Issue 1: User.Roles Attribute Error
- Problem: Code references
User.Roleswhich 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) exceedsrolefield limit (10 chars) - Location:
apps/core/management/commands/seed_comprehensive_data.pyline 876 - Code:
user.role = random.choice(['ENTHUSIAST', 'CASUAL', 'PROFESSIONAL']) - Root Cause:
'PROFESSIONAL'= 12 characters but User.role hasmax_length=10 - Solution Options:
- Fix Data: Change
'PROFESSIONAL'to'PRO'(3 chars) or'EXPERT'(6 chars) - Expand Field: Increase User.role max_length from 10 to 15
- Fix Data: Change
User Model VARCHAR(10) Fields
user_idfield (max_length=10) - line 38 ✅ OKrolefield (max_length=10) - line 50 ⚠️ CONSTRAINT VIOLATION - FIXEDprivacy_levelfield (max_length=10) - line 72 ✅ OKactivity_visibilityfield (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
# 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
showmigrationsoutput 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