BEGIN;
SET CONSTRAINTS ALL DEFERRED;


--------- Se crean las constraints con UPDATE CASCADE
ALTER  TABLE ONLY apex_item_msg DROP CONSTRAINT  "apex_item_msg_fk_item" CASCADE;
ALTER  TABLE ONLY apex_item_nota DROP CONSTRAINT  "apex_item_nota_fk_item" CASCADE;
ALTER  TABLE ONLY apex_molde_operacion DROP CONSTRAINT	"apex_molde_operacion_fk_item" CASCADE;
ALTER  TABLE ONLY apex_item DROP CONSTRAINT	"apex_item_fk_padre"	 CASCADE;
ALTER  TABLE ONLY apex_item_info DROP CONSTRAINT	"apex_item_info_fk_item"  CASCADE;
ALTER  TABLE ONLY apex_item_objeto DROP CONSTRAINT	"apex_item_consumo_obj_fk_item"  CASCADE;
ALTER  TABLE ONLY apex_usuario_grupo_acc_item DROP CONSTRAINT "apex_usu_item_fk_item" CASCADE;
ALTER  TABLE apex_item_msg 			ADD CONSTRAINT 	"apex_item_msg_fk_item" FOREIGN KEY ("item", "item_proyecto") REFERENCES "apex_item" ("item", "proyecto") ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY IMMEDIATE;
ALTER  TABLE apex_item_nota 			ADD CONSTRAINT	"apex_item_nota_fk_item" FOREIGN KEY ("item_proyecto","item") REFERENCES "apex_item" ("proyecto","item") ON DELETE NO ACTION ON UPDATE CASCADE DEFERRABLE INITIALLY IMMEDIATE;
ALTER  TABLE apex_molde_operacion 	ADD CONSTRAINT	"apex_molde_operacion_fk_item" FOREIGN	KEY ("item", "proyecto") REFERENCES	"apex_item"	("item", "proyecto") ON DELETE CASCADE ON UPDATE CASCADE	DEFERRABLE	INITIALLY IMMEDIATE;
ALTER  TABLE apex_item 				ADD CONSTRAINT	"apex_item_fk_padre"	FOREIGN KEY	("padre_proyecto","padre")	REFERENCES "apex_item" ("proyecto","item") ON DELETE NO ACTION	ON	UPDATE CASCADE DEFERRABLE INITIALLY	IMMEDIATE;
ALTER  TABLE apex_item_info 			ADD CONSTRAINT	"apex_item_info_fk_item" FOREIGN	KEY ("item_proyecto","item") REFERENCES "apex_item" ("proyecto","item")	ON	DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY IMMEDIATE;
ALTER  TABLE apex_item_objeto 		ADD CONSTRAINT	"apex_item_consumo_obj_fk_item" FOREIGN KEY ("proyecto","item") REFERENCES	"apex_item"	("proyecto","item") ON DELETE CASCADE ON UPDATE CASCADE	DEFERRABLE	INITIALLY IMMEDIATE;
ALTER  TABLE apex_usuario_grupo_acc_item ADD CONSTRAINT	"apex_usu_item_fk_item"		FOREIGN KEY	("proyecto","item") REFERENCES "apex_item" ("proyecto","item")	ON	DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY IMMEDIATE;
ALTER  TABLE apex_proyecto ADD CONSTRAINT	"apex_proyecto_item_is"		FOREIGN KEY	("proyecto","item_inicio_sesion") REFERENCES "apex_item" ("proyecto","item")	ON	DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY IMMEDIATE;
ALTER  TABLE apex_proyecto ADD CONSTRAINT	"apex_proyecto_item_ps"		FOREIGN KEY	("proyecto","item_pre_sesion") REFERENCES "apex_item" ("proyecto","item")	ON	DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY IMMEDIATE;
ALTER  TABLE apex_proyecto ADD CONSTRAINT	"apex_proyecto_item_ss"		FOREIGN KEY	("proyecto","item_set_sesion") REFERENCES "apex_item" ("proyecto","item")	ON	DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY IMMEDIATE;
ALTER  TABLE apex_objeto_eventos ADD CONSTRAINT	"apex_objeto_eventos_fk_accion_vinculo"		FOREIGN KEY	("proyecto","accion_vinculo_item") REFERENCES "apex_item" ("proyecto","item")	ON	DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY IMMEDIATE;
ALTER  TABLE apex_objeto_ei_cuadro_columna ADD CONSTRAINT	"apex_obj_ei_cuadro_fk_accion_vinculo"		FOREIGN KEY	("objeto_cuadro_proyecto","vinculo_item") REFERENCES "apex_item" ("proyecto","item")	ON	DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY IMMEDIATE;
ALTER  TABLE apex_objeto_ei_formulario_ef ADD CONSTRAINT	"apex_ei_f_ef_fk_accion_vinculo"		FOREIGN KEY	("popup_proyecto","popup_item") REFERENCES "apex_item" ("proyecto","item")	ON	DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY IMMEDIATE;

--------- Se migran los ids de los items
UPDATE apex_item SET item = '30000002' WHERE item = 'Interno/consultas' AND proyecto='tehuelche';
UPDATE apex_item SET item = '30000003' WHERE item = '/Importacion' AND proyecto='tehuelche';
UPDATE apex_item SET item = '30000004' WHERE item = '/Convocatorias' AND proyecto='tehuelche';
UPDATE apex_item SET item = '30000005' WHERE item = '__raiz__' AND proyecto='tehuelche';
UPDATE apex_item SET item = '30000006' WHERE item = '/solicitudes_alumnos' AND proyecto='tehuelche';
UPDATE apex_item SET item = '30000007' WHERE item = '/vinculos' AND proyecto='tehuelche';
UPDATE apex_item SET item = '30000008' WHERE item = '/autovinculo' AND proyecto='tehuelche';
UPDATE apex_item SET item = '30000009' WHERE item = '/entrevistas' AND proyecto='tehuelche';
UPDATE apex_item SET item = '30000010' WHERE item = '/mantenimiento' AND proyecto='tehuelche';
UPDATE apex_item SET item = '30000011' WHERE item = '/Invitado' AND proyecto='tehuelche';
UPDATE apex_item SET item = '30000012' WHERE item = '/tablas_maestras' AND proyecto='tehuelche';
UPDATE apex_item SET item = '30000013' WHERE item = 'alumno' AND proyecto='tehuelche';

COMMIT;
