# UUID Migration Backup Fix

## Issue: "column 'company_uuid' does not exist"

### Problem
The error occurred because we were trying to select from [company_uuid](file:///d:/bookbeach/backend/app/models/company.py#L37-L37) in the [companies](file:///d:/bookbeach/backend/app/models/company.py#L7-L43) table, but at the time this query runs:
1. We had already dropped the old [company_id](file:///d:/bookbeach/backend/app/models/company.py#L37-L37) column from [beach_places](file:///d:/bookbeach/backend/app/models/beach.py#L7-L28) table
2. We were trying to match with the original [company_id](file:///d:/bookbeach/backend/app/models/company.py#L37-L37) values in [beach_places](file:///d:/bookbeach/backend/app/models/beach.py#L7-L28), but those were no longer available

### Root Cause
The approach was incorrect because:
1. We were dropping the foreign key columns before we had a chance to use their values to match with the new UUIDs
2. We didn't have a way to reference the original foreign key values after dropping the columns
3. We were trying to do the operations in the wrong order

### Solution
Completely reorganized the approach to handle foreign key updates correctly by:

1. **Adding backup columns** to store the original numeric foreign key values before dropping them
2. **Populating backup columns** with the original foreign key values
3. **Using backup columns** to match with the new UUID values
4. **Dropping backup columns** after the migration is complete

### Key Changes Made

#### Before (Incorrect):
```sql
-- This was trying to select from company_uuid but we had already dropped company_id
UPDATE beach_places bp SET company_uuid_temp = (SELECT company_uuid FROM companies WHERE company_id = bp.company_id);
```

#### After (Correct):
```sql
-- Added backup columns to preserve original values
ALTER TABLE beach_places ADD COLUMN company_id_backup BIGINT;
-- Populated backup columns with original values
UPDATE beach_places SET company_id_backup = company_id;
-- Used backup columns to match with new UUIDs
UPDATE beach_places bp SET company_uuid_temp = (SELECT company_uuid FROM companies WHERE company_id = bp.company_id_backup);
-- Dropped the original column and renamed temporary column
ALTER TABLE beach_places DROP COLUMN company_id;
ALTER TABLE beach_places RENAME COLUMN company_uuid_temp TO company_id;
-- Cleaned up backup columns
ALTER TABLE beach_places DROP COLUMN company_id_backup;
```

### Files Updated
1. **[05_migrate_key_tables_to_uuid.sql](file://d:/bookbeach/database/scripts/05_migrate_key_tables_to_uuid.sql)** - Completely reorganized the foreign key handling approach with backup columns

### Verification
The migration script should now work correctly without the "column 'company_uuid' does not exist" errors.