# Utils Notification Model Fix - Production Deployment

## Root Cause Found
The production error `OperationalError (1054, "Unknown column 'notifications.related_loan_id' in 'where clause'")` is caused by a **missing model field** in the `utils.Notification` model.

The `reports.Notification` model has a `related_loan` field, but the `utils.Notification` model was missing this field, causing Django queries to fail when trying to access `related_loan_id`.

## Fix Applied
1. **Added `related_loan` field** to `utils.models.Notification`:
   ```python
   related_loan = models.ForeignKey('loans.Loan', on_delete=models.CASCADE, null=True, blank=True, related_name='utils_notifications')
   ```

2. **Created migration** `0015_notification_related_loan.py` to add the database column

## Production Deployment

### Option 1: Upload and Run Fix Script
Upload `fix_utils_notification_model.py` to production and run:
```bash
cd /home/acbptxvs/public_html/branchbusinessadvance.co.ke
python fix_utils_notification_model.py
```

### Option 2: Direct SQL Fix
Run this SQL in production database:
```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`);
```

### Option 3: Upload Model + Migration
1. Upload updated `utils/models.py`
2. Upload `utils/migrations/0015_notification_related_loan.py`
3. Run: `python manage.py migrate utils`

## Files Created
- ✅ `utils/models.py` - Updated with `related_loan` field
- ✅ `utils/migrations/0015_notification_related_loan.py` - Migration file
- ✅ `fix_utils_notification_model.py` - Production deployment script
- ✅ `UTILS_NOTIFICATION_MODEL_FIX.md` - This deployment guide

## Expected Result
After applying this fix:
- ✅ Client delete operations will work without errors
- ✅ No more `OperationalError 1054` on client operations
- ✅ Django can properly query the `related_loan_id` column

## Verification
After deployment, test by trying to delete a client. The operation should complete successfully without the database error.
