-- Script to populate missing UUID foreign key columns with data
-- This is the second part of the final cleanup

-- Populate foreign keys in market_items table
UPDATE market_items mi SET market_uuid_fk = (SELECT market_id FROM markets WHERE market_id::text = mi.market_id::text);

-- Populate foreign keys in bookings table
UPDATE bookings b SET user_uuid_fk = (SELECT user_id FROM users WHERE user_id::text = b.user_id::text);
UPDATE bookings b SET beach_place_uuid_fk = (SELECT beach_place_id FROM beach_places WHERE beach_place_id::text = b.beach_place_id::text);

-- Populate foreign keys in reviews table
UPDATE reviews r SET user_uuid_fk = (SELECT user_id FROM users WHERE user_id::text = r.user_id::text);
UPDATE reviews r SET beach_place_uuid_fk = (SELECT beach_place_id FROM beach_places WHERE beach_place_id::text = r.beach_place_id::text);
UPDATE reviews r SET booking_uuid_fk = (SELECT booking_id FROM bookings WHERE booking_id::text = r.booking_id::text);