-- Emergency SQL fix for missing loan_id column in receipts table
-- Run this directly in your production database

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

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

-- Add index for performance
ALTER TABLE `receipts` 
ADD KEY `receipts_loan_id_idx` (`loan_id`);

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

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