BEGIN;
-- Add constraint to FK
ALTER TABLE dsm_organizationpids DROP CONSTRAINT dsm_organizationpids_pid_fkey;
ALTER TABLE dsm_organizationpids ADD CONSTRAINT dsm_organizationpids_pid_fkey
  FOREIGN KEY (pid) REFERENCES dsm_identities (pid)
  ON UPDATE CASCADE;
  -- Update
  UPDATE dsm_identities
  set pid = substring(pid,5)
  where issuertype ='PIC';
  -- Add original FK
  ALTER TABLE dsm_organizationpids DROP CONSTRAINT dsm_organizationpids_pid_fkey;
  ALTER TABLE dsm_organizationpids ADD CONSTRAINT dsm_organizationpids_pid_fkey
    FOREIGN KEY (pid) REFERENCES dsm_identities (pid);
COMMIT
;