-- Fix inconsistent FK ON DELETE behavior on bulk_operations.performed_by and -- ticket_templates.created_by -- -- Every other user-reference FK in the schema (tickets.created_by/updated_by/ -- assigned_to, ticket_attachments.uploaded_by, ticket_dependencies.created_by, -- recurring_tickets.created_by/assigned_to, api_keys.created_by, etc.) uses -- ON DELETE SET NULL. These two had no ON DELETE clause at all, which -- defaults to RESTRICT — so deleting a user who ever ran a bulk operation or -- created a template hard-fails at the DB level instead of nulling the -- reference, breaking the pattern used everywhere else and potentially -- blocking legitimate user offboarding/cleanup. -- -- bulk_operations.performed_by is NOT NULL today; it must become nullable to -- support SET NULL, matching how every other SET NULL column in the schema -- is defined. -- -- Safe to re-run. ALTER TABLE `bulk_operations` MODIFY COLUMN `performed_by` int(11) DEFAULT NULL; ALTER TABLE `bulk_operations` DROP FOREIGN KEY IF EXISTS `bulk_operations_ibfk_1`; ALTER TABLE `bulk_operations` ADD CONSTRAINT `bulk_operations_ibfk_1` FOREIGN KEY (`performed_by`) REFERENCES `users` (`user_id`) ON DELETE SET NULL; ALTER TABLE `ticket_templates` DROP FOREIGN KEY IF EXISTS `ticket_templates_ibfk_1`; ALTER TABLE `ticket_templates` ADD CONSTRAINT `ticket_templates_ibfk_1` FOREIGN KEY (`created_by`) REFERENCES `users` (`user_id`) ON DELETE SET NULL;