ALTER TABLE AKUZ.T_MOVEMENT_RESULT_VMP_SERVICE_LINK DROP CONSTRAINT if exists fk_t_v022_patient_model_t_movement_result_vmp_service_lin4891;
ALTER TABLE AKUZ.T_MOVEMENT_RESULT_VMP_SERVICE_LINK DROP CONSTRAINT if exists fk_t_v022_patient_model_t_movement_result_vmp_service__ccf8f0c6;

ALTER TABLE AKUZ.T_MOVEMENT_RESULT DROP CONSTRAINT FK_T_V022_PATIENT_MODEL_T_MOVEMENT_RESULT_V022PatientModel;

ALTER TABLE AKUZ.T_V022_PATIENT_MODEL DROP CONSTRAINT if exists PK_T_V022_PATIENT_MODEL;
ALTER TABLE AKUZ.T_V022_PATIENT_MODEL DROP CONSTRAINT if exists t_v022_patient_model_pkey;

ALTER TABLE AKUZ.T_V022_PATIENT_MODEL ADD "v022patientmodelid" uuid NULL;

UPDATE AKUZ.T_V022_PATIENT_MODEL SET "v022patientmodelid" = gen_random_uuid();

ALTER TABLE AKUZ.T_V022_PATIENT_MODEL ALTER COLUMN "v022patientmodelid" SET NOT NULL;

ALTER TABLE AKUZ.T_V022_PATIENT_MODEL ADD CONSTRAINT PK_T_V022_PATIENT_MODEL
PRIMARY KEY (V022PatientModelID);

ALTER TABLE AKUZ.T_MOVEMENT_RESULT_VMP_SERVICE_LINK RENAME COLUMN V022PatientModel TO V022PatientModelOld;

ALTER TABLE AKUZ.T_MOVEMENT_RESULT_VMP_SERVICE_LINK ADD "v022patientmodel" uuid NULL;

ALTER TABLE AKUZ.T_MOVEMENT_RESULT_VMP_SERVICE_LINK ADD CONSTRAINT FK_T_V022_PATIENT_MODEL_T_MOVEMENT_RESULT_VMP_SERVICE__CCF8F0C6
FOREIGN KEY("v022patientmodel") REFERENCES AKUZ.T_V022_PATIENT_MODEL (V022PatientModelID);

UPDATE AKUZ.T_MOVEMENT_RESULT_VMP_SERVICE_LINK AS mrv
SET V022PatientModel = vpm.V022PatientModelID
FROM AKUZ.T_V022_PATIENT_MODEL AS vpm
WHERE vpm.IDMPAC = mrv.V022PatientModelOld;

alter table AKUZ.T_MOVEMENT_RESULT_VMP_SERVICE_LINK drop column V022PatientModelOld;

ALTER TABLE AKUZ.T_MOVEMENT_RESULT RENAME COLUMN V022PatientModel TO V022PatientModelOld;

ALTER TABLE AKUZ.T_MOVEMENT_RESULT ADD "v022patientmodel" uuid NULL;

ALTER TABLE AKUZ.T_MOVEMENT_RESULT ADD CONSTRAINT FK_T_V022_PATIENT_MODEL_T_MOVEMENT_RESULT_V022PatientModel
FOREIGN KEY("v022patientmodel") REFERENCES AKUZ.T_V022_PATIENT_MODEL (V022PatientModelID);

UPDATE AKUZ.T_MOVEMENT_RESULT AS mr
SET V022PatientModel = vpm.V022PatientModelID
FROM AKUZ.T_V022_PATIENT_MODEL AS vpm
WHERE vpm.IDMPAC = mr.V022PatientModelOld;

alter table AKUZ.T_MOVEMENT_RESULT drop column V022PatientModelOld;

ALTER TABLE AKUZ.T_V022_PATIENT_MODEL ALTER COLUMN "mpacname" SET NOT NULL;

ALTER TABLE AKUZ.T_V022_PATIENT_MODEL ALTER COLUMN "datebeg" SET NOT NULL;

ALTER TABLE AKUZ.T_V022_PATIENT_MODEL ALTER COLUMN "idmpac" SET NOT NULL;

ALTER TABLE AKUZ.T_N002_OnkStad ALTER COLUMN "kod_st" SET DATA TYPE varchar(32);