# UUID Migration Correct Order 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 that column had already been dropped. 

### Root Cause
The approach was incorrect because:
1. We were dropping the primary key column ([company_id](file:///d:/bookbeach/backend/app/models/company.py#L37-L37)) from the [companies](file:///d:/bookbeach/backend/app/models/company.py#L7-L43) table too early in section 4
2. We were trying to reference [company_uuid](file:///d:/bookbeach/backend/app/models/company.py#L37-L37) after it had been dropped
3. The correct order should be:
   - Add UUID columns
   - Rename UUID columns to original names (in section 5)
   - Drop old columns (in section 5, after renaming)

### Solution
Fixed the order of operations by:

1. **Not dropping primary key columns** from key tables in section 4
2. **Dropping primary key columns** from key tables in section 5, after renaming UUID columns
3. **Using the correct column names** in all references

### Key Changes Made

#### Before (Incorrect):
```sql
-- Section 4: Dropping company_id from companies table (WRONG)
ALTER TABLE companies DROP COLUMN company_id;

-- Section 5: Renaming company_uuid to company_id (TOO LATE)
ALTER TABLE companies RENAME COLUMN company_uuid TO company_id;
```

#### After (Correct):
```sql
-- Section 4: NOT dropping company_id from companies table
-- NOTE: Do not drop company_id yet, it will be dropped after renaming in section 5

-- Section 5: Renaming company_uuid to company_id and THEN dropping old column
ALTER TABLE companies RENAME COLUMN company_uuid TO company_id;
ALTER TABLE companies ADD PRIMARY KEY (company_id);
-- NOTE: The old company_id column is now dropped as part of the rename operation
```

### Files Updated
1. **[05_migrate_key_tables_to_uuid.sql](file://d:/bookbeach/database/scripts/05_migrate_key_tables_to_uuid.sql)** - Fixed the order of operations for dropping columns

### Verification
The migration script should now work correctly without the "column 'company_uuid' does not exist" errors.