-- ============================================================
-- Migration 001: Fix certificates.form_id foreign key
-- The original FK only pointed to lab_work_forms(form_id),
-- which breaks for balance_lab_forms and humidity_lab_forms.
-- Solution: drop the FK, keep form_id as a nullable INT with
-- no FK constraint (since it spans multiple tables).
-- ============================================================
USE `metlab_db`;

SET FOREIGN_KEY_CHECKS = 0;

ALTER TABLE `certificates`
  MODIFY COLUMN `form_id` INT UNSIGNED NULL
  COMMENT 'ID in the lab-specific form table. No FK since table varies by lab.';

-- Drop FK constraint if it exists (name may vary by MySQL version/install)
-- Run each; ignore "Can't DROP" errors — they just mean the FK had a different name.
ALTER TABLE `certificates` DROP FOREIGN KEY IF EXISTS `certificates_ibfk_2`;
ALTER TABLE `certificates` DROP FOREIGN KEY IF EXISTS `fk_cert_form`;

SET FOREIGN_KEY_CHECKS = 1;
