-- Emergency SQL fix for missing related_loan_id column in notifications table
-- Run this directly in your production database

-- Check if notifications table exists
SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_SCHEMA = DATABASE() 
  AND TABLE_NAME = 'notifications';

-- Check if column exists first
SELECT COLUMN_NAME 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_SCHEMA = DATABASE() 
  AND TABLE_NAME = 'notifications' 
  AND COLUMN_NAME = 'related_loan_id';

-- Add the missing column if it doesn't exist
ALTER TABLE `notifications` 
ADD COLUMN `related_loan_id` char(32) NULL;

-- Add index for performance
ALTER TABLE `notifications` 
ADD KEY `notifications_related_loan_id_idx` (`related_loan_id`);

-- Try to add foreign key constraint (optional - may fail if referential integrity issues)
-- ALTER TABLE `notifications` 
-- ADD CONSTRAINT `notifications_related_loan_id_fk` 
-- FOREIGN KEY (`related_loan_id`) REFERENCES `loans` (`id`) ON DELETE CASCADE;

-- Verify the column was added
DESCRIBE `notifications`;
