DROP FUNCTION IF EXISTS vclib.t_entities_attributes_delete CASCADE;
CREATE  FUNCTION vclib.t_entities_attributes_delete()
RETURNS TRIGGER AS $$
BEGIN
    INSERT INTO ies.t_entity_attributes_change(
        entityattributeschangeid, entityid, "index", attributetype, "name", description, systemtype, maxlength, "precision", "scale", defaultformat, iscalculated, isflag, isrequired,
        mappingtype, mappingname, save, linkattributename, choiceattributedata, isstored, attrlexem, pathsattributes, iscashed, isadditional, mappingtable, mappingreftable, parametersdata, customsdata, requiredattributes,
        sortingattribute, enumtype, isidentity, iscustom, removeaction, isprimarykey, entitiesattributesid, editdate, "action"
    )
    SELECT
        gen_random_uuid(), OLD.entityid, OLD.index, OLD.attributetype, OLD.name, OLD.description, OLD.systemtype, OLD.maxlength, OLD.precision, OLD.scale, OLD.defaultformat, OLD.iscalculated, OLD.isflag, OLD.isrequired,
        OLD.mappingtype, OLD.mappingname, OLD.save, OLD.linkattributename, OLD.choiceattributedata, OLD.isstored, NULL, NULL, OLD.iscashed, OLD.isadditional, OLD.mappingtable, OLD.mappingreftable, OLD.parametersdata, OLD.customsdata, OLD.requiredattributes,
        OLD.sortingattribute, OLD.enumtype, OLD.isidentity, OLD.iscustom, OLD.removeaction, OLD.isprimarykey, OLD.entitiesattributesid, NOW(), 2;
    
	
    RETURN OLD; 
END;
$$ LANGUAGE plpgsql;


DROP TRIGGER IF EXISTS t_entities_attributes_after_delete ON vclib.t_entities_attributes;
CREATE TRIGGER t_entities_attributes_after_delete
AFTER DELETE ON vclib.t_entities_attributes
FOR EACH ROW EXECUTE FUNCTION vclib.t_entities_attributes_delete();