# Related Loan ID Column Fix - Production Deployment

## Issue
Production error: `OperationalError (1054, "Unknown column 'notifications.related_loan_id' in 'where clause'")` on client delete operations.

## Root Cause
The `utils_notification` table is missing the `related_loan_id` column in production, causing Django queries to fail when trying to delete clients.

## Solution
Deploy the emergency fix script to add the missing column.

## Deployment Steps

### 1. Upload Fix Script
Upload `deploy_related_loan_id_fix.py` to your production server.

### 2. Run the Fix
```bash
# Navigate to your Django project directory
cd /home/acbptxvs/public_html/branchbusinessadvance.co.ke

# Run the emergency fix
python deploy_related_loan_id_fix.py
```

### 3. Verify Fix
The script will automatically verify the column was added successfully.

## What the Fix Does
- ✅ Adds `related_loan_id` column to `utils_notification` table
- ✅ Creates index for performance
- ✅ Attempts to add foreign key constraint (graceful fallback if fails)
- ✅ Verifies the fix was applied

## Expected Output
```
2025-08-29 XX:XX:XX - INFO - Adding related_loan_id column to utils_notification table...
2025-08-29 XX:XX:XX - INFO - related_loan_id column added successfully
2025-08-29 XX:XX:XX - INFO - Index added successfully
2025-08-29 XX:XX:XX - INFO - Foreign key constraint added successfully
2025-08-29 XX:XX:XX - INFO - SUCCESS: related_loan_id column exists in utils_notification
2025-08-29 XX:XX:XX - INFO - Production fix completed successfully!
```

## Post-Fix Verification
After running the fix:
1. Try deleting a client - should work without errors
2. Check the log file `related_loan_id_fix.log` for details
3. Verify no more OperationalError 1054 on client operations

## Safety
- Script checks if column already exists before adding
- Uses Django's database connection for safety
- Comprehensive logging for troubleshooting
- Graceful error handling

## Emergency Contact
If issues persist, the column can be added manually via SQL:
```sql
ALTER TABLE `utils_notification` ADD COLUMN `related_loan_id` char(32) NULL;
ALTER TABLE `utils_notification` ADD KEY `utils_notification_related_loan_id_idx` (`related_loan_id`);
```
