-- Add created_by column to branch-specific expenses table
ALTER TABLE `{branch_code}_expenses` 
ADD COLUMN `created_by` INT NULL AFTER `status`;

-- Update existing records to use a default user (admin user with ID 1)
UPDATE `{branch_code}_expenses` SET `created_by` = 1 WHERE `created_by` IS NULL;

-- Make the column NOT NULL after updating existing records
ALTER TABLE `{branch_code}_expenses` 
MODIFY COLUMN `created_by` INT NOT NULL;

-- Add the foreign key constraint
ALTER TABLE `{branch_code}_expenses`
ADD CONSTRAINT `{branch_code}_expenses_created_by_fk` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`); 