ALTER TABLE akuz.t_schet_sluch_link_f014 DROP CONSTRAINT fk_t_f014_deny_reason_t_schet_sluch_link_f014_f014denyreason;
ALTER TABLE akuz.t_hschet_sluch_link_f014 DROP CONSTRAINT fk_t_f014_deny_reason_t_hschet_sluch_link_f014_f014denyreason;
ALTER TABLE akuz.t_f014_deny_reason DROP CONSTRAINT pk_t_f014_deny_reason;


ALTER TABLE akuz.t_f014_deny_reason ADD COLUMN f014denyreasonid UUID;
UPDATE akuz.t_f014_deny_reason SET f014denyreasonid = gen_random_uuid();
ALTER TABLE akuz.t_f014_deny_reason ALTER COLUMN f014denyreasonid SET NOT NULL;
ALTER TABLE akuz.t_f014_deny_reason ADD CONSTRAINT pk_t_f014_deny_reason PRIMARY KEY (f014denyreasonid);


ALTER TABLE akuz.t_schet_sluch_link_f014 RENAME COLUMN f014denyreason TO f014denyreason_old;
ALTER TABLE akuz.t_hschet_sluch_link_f014 RENAME COLUMN f014denyreason TO f014denyreason_old;


ALTER TABLE AKUZ.T_SCHET_SLUCH_LINK_F014 ADD "f014denyreason" uuid NULL;

UPDATE AKUZ.T_SCHET_SLUCH_LINK_F014
SET F014DenyReason = fdr.F014DenyReasonID
from AKUZ.T_F014_DENY_REASON fdr
where fdr.Kod = F014DenyReason_old;

ALTER TABLE AKUZ.T_SCHET_SLUCH_LINK_F014 ALTER COLUMN "f014denyreason" SET NOT NULL;

ALTER TABLE AKUZ.T_SCHET_SLUCH_LINK_F014 ADD CONSTRAINT FK_T_F014_DENY_REASON_T_SCHET_SLUCH_LINK_F014_F014DenyReason
FOREIGN KEY("f014denyreason") REFERENCES AKUZ.T_F014_DENY_REASON (F014DenyReasonID);


ALTER TABLE AKUZ.T_HSCHET_SLUCH_LINK_F014 ADD "f014denyreason" uuid NULL;

UPDATE AKUZ.T_HSCHET_SLUCH_LINK_F014
SET F014DenyReason = fdr.F014DenyReasonID
from AKUZ.T_F014_DENY_REASON fdr
where fdr.Kod = F014DenyReason_old;

ALTER TABLE AKUZ.T_HSCHET_SLUCH_LINK_F014 ALTER COLUMN "f014denyreason" SET NOT NULL;

ALTER TABLE AKUZ.T_HSCHET_SLUCH_LINK_F014 ADD CONSTRAINT FK_T_F014_DENY_REASON_T_HSCHET_SLUCH_LINK_F014_F014DenyReason
FOREIGN KEY("f014denyreason") REFERENCES AKUZ.T_F014_DENY_REASON (F014DenyReasonID);


ALTER TABLE AKUZ.T_SCHET_SLUCH_LINK_F014 DROP COLUMN f014denyreason_old;
ALTER TABLE AKUZ.T_HSCHET_SLUCH_LINK_F014 DROP COLUMN f014denyreason_old;