# Upload 500 Error Fix - Complete ## Date: December 19, 2025 ## Problem File uploads were failing with HTTP 500 error: ``` POST http://localhost:5000/api/admin/upload 500 (Internal Server Error) Error: "Failed to save uploaded files" ``` ## Root Cause ### Backend Log Error ``` [error]: Database insert failed for file: { "filename":"18498-1766201320693-912285946.jpg", "error":"invalid input syntax for type integer: \"admin-default\"" } ``` ### Database Type Mismatch - `adminusers.id` = **TEXT** (value: `"admin-default"`) - `uploads.uploaded_by` = **INTEGER** ❌ - `media_folders.created_by` = **INTEGER** ❌ When the upload route tried to insert `req.session.user.id` (`"admin-default"`) into the INTEGER column, PostgreSQL rejected it. ## Solution ### Database Schema Fix Changed column types to match the `adminusers.id` type: ```sql -- Fix uploads table ALTER TABLE uploads ALTER COLUMN uploaded_by TYPE TEXT; -- Fix media_folders table ALTER TABLE media_folders ALTER COLUMN created_by TYPE TEXT; ``` ### Verification ``` uploads.uploaded_by: integer → text ✅ media_folders.created_by: integer → text ✅ ``` ## Files Changed ### Database - ✅ `uploads.uploaded_by`: INTEGER → TEXT - ✅ `media_folders.created_by`: INTEGER → TEXT ### Migration File Created - ✅ `backend/migrations/fix-uploaded-by-type.sql` ### Backend Code - ✅ No changes needed (already using `req.session.user?.id`) ## Why This Happened The original schema was designed expecting numeric user IDs, but the authentication system uses text-based IDs (`"admin-default"`). This type mismatch wasn't caught until file upload was tested. ## Testing ### Before Fix ``` ❌ Upload fails with 500 error ❌ Database insert error ❌ Files uploaded to disk but not in database ❌ User sees "Upload failed" message ``` ### After Fix ``` ✅ Upload succeeds ✅ Database insert works ✅ Files tracked properly ✅ User sees success message ``` ## Test Steps 1. Navigate to 2. Click **"Upload Files"** button 3. Select one or more images (JPG, PNG, GIF, WebP) 4. Watch progress bar complete 5. See success alert: "Successfully uploaded X file(s)!" 6. Files appear in the media grid ## Related Tables Fixed Both tables that reference user IDs: - `uploads` - Tracks uploaded files - `media_folders` - Tracks folder creators Both now use TEXT to match `adminusers.id`. ## Migration Safety The ALTER TABLE commands are safe because: - ✅ No existing data (tables are new) - ✅ TEXT can hold any INTEGER value - ✅ No foreign key constraints broken - ✅ Instant operation (no data conversion needed) ## Summary **Upload functionality is now fully operational!** The database schema mismatch between user IDs (TEXT) and foreign keys (INTEGER) has been resolved. All file uploads and folder creation operations will now work correctly with the text-based user IDs from the authentication system.