-- Enforce one treatment detail per lab case (1:1 via join table).
-- Keep the earliest-linked detail when duplicate rows exist for the same case.

DELETE FROM "lab_case_details" lcd
WHERE lcd.ctid NOT IN (
  SELECT MIN(inner_lcd.ctid)
  FROM "lab_case_details" inner_lcd
  GROUP BY inner_lcd."labCaseId"
);

CREATE UNIQUE INDEX "lab_case_details_labCaseId_key" ON "lab_case_details"("labCaseId");
