# UUID Migration Latest Fix

## Issue: "column 'company_uuid' does not exist"

### Problem
The error occurred because we were trying to select from [companies](file:///d:/bookbeach/backend/app/models/company.py#L7-L43).[company_uuid](file:///d:/bookbeach/backend/app/models/company.py#L37-L37) in section 7 of the migration script, but at that point:
1. We hadn't yet renamed [company_uuid](file:///d:/bookbeach/backend/app/models/company.py#L37-L37) to [company_id](file:///d:/bookbeach/backend/app/models/company.py#L37-L37) (which happens in section 5)
2. The column [company_uuid](file:///d:/bookbeach/backend/app/models/company.py#L37-L37) didn't exist because we had already dropped it in section 4

### Root Cause
The order of operations was incorrect:
1. Section 7 (foreign key updates) was trying to reference columns that would only exist after section 5 (primary key renames)
2. We were using the wrong column name in our SELECT statements

### Solution
Fixed the order of operations and corrected the column references:

1. **Reorganized the script structure** to ensure the correct sequence:
   - Section 1: Add UUID columns to key tables
   - Section 2: Populate foreign key columns with UUIDs
   - Section 3: Drop constraints
   - Section 4: Drop old columns
   - Section 5: Rename primary key columns and set as primary keys
   - Section 6: Update foreign key columns in referencing tables
   - Section 7: Update foreign key columns that reference system tables

2. **Fixed column references** in section 7 to use the correct column names:
   - Changed `SELECT company_uuid FROM companies` to `SELECT company_id FROM companies`
   - Changed `SELECT beach_place_uuid FROM beach_places` to `SELECT beach_place_id FROM beach_places`
   - And similarly for all other references

3. **Updated the approach** in section 7:
   - Instead of selecting from the temporary UUID columns, we now select from the renamed columns
   - This ensures we're referencing the correct columns that exist at the time of execution

## Files Updated
1. **[05_migrate_key_tables_to_uuid.sql](file://d:/bookbeach/database/scripts/05_migrate_key_tables_to_uuid.sql)** - Fixed the column reference issues and reorganized the operations order

## Verification
The migration script should now work correctly without the "column does not exist" errors.