DELETE `module_settings` FROM `module_settings` LEFT JOIN `module_instances` USING(`module_instance_id`) WHERE `module_instances`.`module_instance_id` IS NULL;
ALTER TABLE `module_settings` ADD CONSTRAINT `fk_module_instance_id` FOREIGN KEY (`module_instance_id`) REFERENCES `module_instances` (`module_instance_id`) ON DELETE CASCADE ON UPDATE CASCADE;
