# Notification Model Conflict Fix

## Problem

When attempting to delete a client, the following error occurs:

```
OperationalError at /clients/2efd58ca-8735-4416-bfc0-1ad2a68e74b6/delete/ 
(1054, "Unknown column 'notifications.related_application_id' in 'where clause'")
```

This error occurs during a POST request to `/clients/{uuid}/delete/` and is raised in the `users.views.client_delete` function.

## Root Cause

The issue is caused by a conflict between two `Notification` models in the codebase:

1. `reports.models.Notification` - This model has `related_application` and `related_loan` fields and uses `db_table='notifications'`
2. `utils.models.Notification` - This model does not have these fields but is being queried with them

When the `client_delete` function is executed, it attempts to filter notifications using `related_application_id`, but this column doesn't exist in the database table.

## Solution

Two scripts have been created to fix this issue:

### 1. `fix_notification_model.py`

This script adds the missing `related_application_id` column to the `notifications` table:

```python
python fix_notification_model.py
```

This is a quick fix that will allow the client deletion to work immediately.

### 2. `fix_notification_models_conflict.py`

This is a more comprehensive solution that:

1. Adds the missing column to both potential notification tables (`notifications` and `utils_notification`)
2. Updates the `utils.Notification` model in memory to include the `related_application` field
3. Creates a migration file to permanently add the field to the model
4. Updates the `utils/models.py` file to include the field definition

```python
python fix_notification_models_conflict.py
```

After running this script, you should:

1. Apply the migration: `python manage.py migrate utils`
2. Restart the Django server

## Verification

To verify the fix:

1. Try deleting a client through the web interface
2. Check the database structure to confirm the column exists:

```sql
DESCRIBE notifications;
```

or

```sql
SELECT column_name FROM information_schema.columns WHERE table_name='notifications';
```

## Rollback Plan

If issues persist:

1. Restore the backup of `utils/models.py` (created as `utils/models.py.bak`)
2. Revert the migration: `python manage.py migrate utils 0005_alter_receipt_payment_method`
3. Contact the development team for further assistance